A detail of the Java Integer wrapper class boxing

Source: Internet
Author: User
Tags wrapper

Java has eight basic data types, each of which has a corresponding wrapper class, such as an integer corresponding to int. Starting with jdk1.5, Java introduces automatic unboxing, which can be directly assigned as an integer i = 20, and the compiler automatically converts it to integer i = integer.valueof (20) for boxing, and unpacking the Int J The form of = i is converted to int j = I.intvalue ().

There's a detail in the box, and if you're not careful, it's easy to make mistakes, take a look:

Integer i == integer.valueof (j ==);

The above code output is

True

There seems to be no problem, then we have the same form, the number 20 is replaced by 200, that is

i =integer.valueof (200);

The same judgment, the output becomes:

False

What is this for?


Let's be clear, after the compiler compiles, the integer i = 20 is converted to integer i = integer.valueof (20), and the definition of integer j = integer.valueof (20) is exactly the same, Then why did the 20 change to 200 after the judgment result is different?
Let's take a look at the interior of the integer.valueof (int i) method:

     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);    }


It can be seen that when I is within a certain interval, a value in the cache array Integercache.cache is returned directly, and a new integer object is out of range. Here we can probably conclude: 20 in the cache range so directly with the cache, but 200 out of the buffer room so new object, and the original object's address is certainly not the same, so return false
And then look at Integercache, which is an integer private static inner class, defined as follows:

    Private Static classIntegercache {Static Final intLow =-128; Static Final intHigh ; Static FinalInteger cache[]; Static {            //High value is configured by property            intH = 127; String Integercachehighpropvalue=Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if(Integercachehighpropvalue! =NULL) {                inti =parseint (Integercachehighpropvalue); I= Math.max (i, 127); //Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value-(-Low )); } High=h; Cache=Newinteger[(high-low) + 1]; intj =Low ;  for(intk = 0; K < Cache.length; k++) Cache[k]=NewInteger (j + +); }        PrivateIntegercache () {}}

It can be seen that the default buffer between the -128~127, then under what circumstances will modify this range, modify the parameters of a virtual machine, through the code can also be seen, Set this cache upper limit Java.lang.Integer.IntegerCache.high value can not be less than 127, less than the words will be given 127, thereby invalidating.
So how do you set this value? Let's look at the JDK source code to explain how integercache this static inner class:

Cache to support the object identity semantics of autoboxing in values between-128 and 127 (inclusive) as required by JL S. The cache is initialized on first usage. The size of the cache is controlled by the-xx:autoboxcachemax= option. During VM initialization, Java.lang.Integer.IntegerCache.high property is set and saved in the private system Properti Es in the Sun.misc.VM class.


It probably means:

Caches 128 to 127 (inclusive) numbers for automatic boxing. The cache is initialized the first time it is used. The size can be specified by the JVM parameter-xx:autoboxcachemax=option. This value is set to the Java.lang.Integer.IntegerCache.high property and is stored as a private system property in Sun.misc.vm.class when the JVM is initialized.

It can be concluded that the high value of this cache is specified by the JVM parameter-xx:autoboxcachemax= option.

The above JDK source code from jdk1.7, different versions of the implementation slightly different, but the idea is consistent.

This idea of sharing common objects has a name, called the flyweight, which is named "the shared lightweight element." Other wrapper classes such as Boolean, Byte, short, Long, and charactor all have similar implementations.

One detail of the Java Integer wrapper class boxed

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.