Java constant pool detailed integer cache

Source: Internet
Author: User

A Java question to output results
public class Integertest {public static void main (string[] args) {objpooltest ();}  public static void Objpooltest () {integer I1 = 40;integer i2 = 40;integer i3 = 0;integer i4 = new Integer (+); Integer i5 = New Integer (I6), integer = new integer (0); System.out.println ("i1=i2 \ T" + (I1 = = i2)); System.out.println ("i1=i2+i3 \ T" + (I1 = = i2 + i3)); System.out.println ("i4=i5 \ T" + (I4 = = i5)); System.out.println ("i4=i5+i6 \ T" + (I4 = = i5 + I6)); System.out.println ();}}

The output is

I1=i2 Truei1=i2+i3 Truei4=i5 falsei4=i5+i6 True
Seemingly easy problem, but the console output of the result is exactly the opposite of what we thought, and we wondered why.
Finally through the online search that Java to improve performance provides the same as the string class object pool mechanism, of course, Java's eight basic types of wrapper class (packaging type) also has an object pool mechanism. The integer I1=40;java executes the code into an integer i1=integer.valueof (40) at compile time, by viewing the source code discovery
/** * Returns A <tt>Integer</tt> instance representing the specified * <tt>int</tt> value. * If A new <tt>Integer</tt> instance is not required, this method * should generally being used in Prefe Rence to the constructor * {@link #Integer (int.)}, as this method was likely to yield * significantly better space A     nd time performance by caching * frequently requested values.     * * @param i an <code>int</code> value.     * @return A <tt>Integer</tt> instance representing <tt>i</tt>.    * @since 1.5 */public static Integer valueOf (int i) {final int offset = 128;    if (i >= -128 && i <= 127) {//must cache return integercache.cache[i + offset];    } return new Integer (i); }

Integer.valueof () has an inner class integercache (similar to a constant array, also called an object pool) that maintains an integer array cache with a length of (128+127+1) = There is also a static block in the 256;integer class
static {for        (int i = 0; i < cache.length; i++)        cache[i] = new Integer (i-128);    }

As you can see from this static block, the integer cache data for the value "128-127" has been created by default.   So when you use an integer i1=40, the JVM will directly find a reference to that value in the object pool. That is, when an integer object is declared in this way, the JVM first looks for an object with a value of 40 in the cache pool of the integer object, if there is a reference to return the object directly, and if not, creates an object with new keyword and returns the reference address of the object. because "= =" in Java compares whether two objects are the same reference (that is, compare memory addresses), I2 and I2 are all references to the same object, so I1==i2 result is "true", and the I4=new Integer (40), I5=new, created using new mode  Integer (40), although their values are equal, each time they recreate the new integer object and are not put into the object pool, they are not the same reference, and the output is false.    For I1==i2+i3, i4==i5+i6 The result is true, because, Java mathematical calculation is in the memory stack operation, Java will be i5, I6 unboxing operation, in fact, compared to the basic type (40=40+0), their values are the same, so the result is true. Well, I want to say here that everyone should have a further understanding of the integer object pool, I asked in Connaught if the 40 changed to 400 guess what will output?
I1=i2 Falsei1=i2+i3 Truei4=i5 falsei4=i5+i6 True
This is because integer i1=400,integer i2=400 their values have exceeded the range of the constant pool, and the JVM creates new objects (that is, Integer i1=new integer (400)) for I1 and I2, so they are not the same reference.
Reprinted from: HTTP://WWW.TUICOOL.COM/ARTICLES/AQYAYNM

Java constant pool detailed integer cache

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.