JDK Source Code Analysis----Integer

Source: Internet
Author: User
Tags array length int size static class

1. Integer Caching Technology


After JDK1.5 Java introduced automatic boxing and automatic unpacking technology,

Integer ina = 2;
Integer inb = 2;
SYSTEM.OUT.PRINTLN ("After packing" + (ina = INB));
Integer inc =;
Integer ind =;
System.out.println ("Boxed after" + (inc = = IND));

The results above are true and false respectively. Why two boxing results are different. This is because the Java integer has an inner class when it is implemented,

    private static class Integercache {
        static final int low = -128;
        static final int high;
        Static final Integer cache[];

        static {
            //high value May is configured by property
            int h = 127;
            String Integercachehighpropvalue =
                sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");
            if (integercachehighpropvalue!= null) {
                int i = parseint (integercachehighpropvalue);
                i = Math.max (i, 127);
                Maximum array size is integer.max_value
                h = math.min (i, Integer.max_value-(-low)-1);
            High = h;

            cache = new Integer[(high-low) + 1];
            int j = Low;
            for (int k = 0; k < cache.length; k++)
                cache[k] = new Integer (j + +);
        }

        Private Integercache () {}
    }

It is not hard to find that integers between 128 and 127 are placed in an array called cache the first time they are used (when the class is loaded). Of course, this size range can change the JVM's settings to change it.

Also, an integer static method valueof (int i) uses the array.

   public static Integer valueof (int i) {
        assert integercache.high >= 127;
        if (i >= integercache.low && i <= integercache.high) return
            Integercache.cache[i + (-integercache.low) ];
        return new Integer (i);
    }

2. A string representation of an integer in a given system


toString (int i, int radix)

Char buf[] = new char[33];
		Boolean negative = (i < 0);
		int charpos =;
		if (negative)
		{
			i = i
		}

		while (i >= radix)
		{
			buf[charpos--] = Digits[i%radix];
			i = I/radix;
		}
		Buf[charpos] = digits[i];
		if (negative)
		{
			Buf[--charpos] = '-';
		}
		return new String (BUF, Charpos, (33-charpos));

The above is the core code, in fact, in the JDK code is unified into negative processing. I think this is not in line with the custom, this time to unify into positive treatment. As for why the array length of 33 is also apparent, because the Java int has 32 bits, plus the possible symbol bit.

3. The length of an integer


static int stringsize (int x)

This function is not a public permission function and is used as an internal tool method.

    final static int [] sizetable = {9, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.max_value};<
                                      c3/>//Requires positive x
    static int stringsize (int x) {for
        (int i=0;; i++)
            if (x <= sizetable[i])
                R Eturn i+1;
    }

The implementation of this method is very ingenious, avoid division, and so on, judge the condition is simple, high efficiency (static field analysis, rather than responsible for logical judgment can significantly improve the effect). (int max length only 10)

4. The implementation of toString ()


It may be felt that the above ToString (int i, int radix) is already a general-purpose algorithm, but the JDK does not (that is, the radix is 10), but uses more efficient methods.

   public static String toString (int i) {
        if (i = = Integer.min_value) return
            " -2147483648";
        int size = (I < 0)? Stringsize (i) + 1:stringsize (i);
        char[] buf = new Char[size];
        GetChars (i, size, buf);
        return new String (buf, True);
    }

The core of this algorithm is the implementation of GetChars, where an integer is efficiently stored in a char array.

static void GetChars (int i, int index, char[] buf) {
        int q, r;
        int charpos = index;
        char sign = 0;

        if (I < 0) {
            sign = '-';
            i =-I;
        }

   while (i >= 65536) {
            q = i/100;
        Really:r = i-(q *);
            R = i-((Q << 6) + (q << 5) + (Q << 2));
            i = q;
            BUF [--charpos] = digitones[r];
            BUF [--charpos] = Digittens[r];
        }

This process is a large number of parts, of which Digitones and Digittens are as follows,

    Final static char [] Digittens = {
        ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1, ' 1 ', ' 1 ', ' 1 ',
        ' 1 ', ' 1 ', ' 1 ', ' 1 ',
        ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', '
        3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ' , ' 3 ',
        ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 5 ', '
        5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 6 '
        , ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ',
        ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 8 ', ' 8 ', ' 8 ', ' 8 '
        ', ' 8 ', ' 8 ', ' 8 ', ' 8 ', ' 8 ', ' 8 ', ' 9 ', ' 9 ', ' 9 '
        , ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ', ' 9 ',
        };

    Final static char [] Digitones = {
        ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2, ' 3 ', ' 4 ', ' 5 ',
        ' 6 ', ' 7 ', ' 8 ', ' 9 ',
        ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', '
        0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ' , ' 9 ',
        ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', '
        1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 '
        , ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',
        ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 '
        ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 '
        , ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ', ' 9 ',
        };

The thought in the while part above is that digitones is a single digit, Digittens represents 10 bits, each time R can iterate two bits (R is divided by 100). As for the shift operation, it is to increase the speed of operation, q*100 = q* (2^6) + q* (2^5) + q* (2^2) = 64q+32q+4q.

And then take a quicker approach to decimals,

    for (;;) {
            q = (i * 52429) >>> (16+3);
            R = i-((Q << 3) + (q << 1));  R = I (q*10)
            ... BUF [--charpos] = digits [R];
            i = q;
            if (i = = 0) break;
        

The function of the above operation is q to get the value of I truncated single-digit. (q = I/10). As for the use of these complex shifts, the aim is to improve speed (>>> unsigned right shift).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.