Explanation of Java constant pool: Integer cache, constant integer

Source: Internet
Author: User

Explanation of Java constant pool: Integer cache, constant integer

A Java question to evaluate the output result
 
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(40);Integer i5 = new Integer(40);Integer i6 = 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 result is

 

i1=i2 truei1=i2+i3 truei4=i5 falsei4=i5+i6 true
It seems Easy, but the Result output by the Console is exactly the opposite of what we think. We are confused. Why?
Finally, I learned through online search that Java provides the same object pool mechanism as the String class to improve performance. Of course, the eight basic types of Java Packaging classes also have the object pool mechanism. Integer i1 = 40; Java will encapsulate the Code into Integer i1 = Integer. valueOf (40) during compilation; it is found by viewing the Source Code
   /**     * 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 be used in preference to the constructor     * {@link #Integer(int)}, as this method is likely to yield     * significantly better space and 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 internal class IntegerCache (similar to a constant array, also called an object pool), which maintains an Integer array cache with a length of (128 + 127 + 1) = 256; there is also a Static Block (Static Block) in the Integer class)
static {        for(int i = 0; i < cache.length; i++)        cache[i] = new Integer(i - 128);    }

 

From this static block, we can see that Integer has created the Integer cache data of the value [-128-127] by default. Therefore, when Integer i1 = 40 is used, JVM will directly find a reference to this value in the object pool. That is to say, when an Integer object is declared in this way, JVM first searches for an object with a wooden value of 40 in the buffer pool of the Integer object. If yes, the reference of this object is directly returned. If no, create an object using New keyword and return the reference address of the object. In Java, [=] compares whether two objects are referenced in the same reference (that is, compare the memory address). i2 and i2 both reference the same object, so i1 = i2 the result is "true", while i4 = new Integer (40) and i5 = new Integer (40) created using the new method, although their values are equal, however, each time a new Integer object is created, it is not put into the object pool, so they are not the same reference and the output is false. The result of i1 = i2 + i3, i4 = i5 + i6 is True because Java's mathematical computing is operated in the memory stack, java will unpack i5 and i6. In fact, the basic type (40 = 40 + 0) is compared, and their values are the same, so the result is True. Well, I 'd like to say that everyone should have a better understanding of the Integer Object pool here. I asked Nono what to output if I changed 40 to 400?
 
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, JVM creates new objects for i1 and i2 (I .e. Integer i1 = new Integer (400), so they are not the same reference.
 
 
Reprinted from: http://www.tuicool.com/articles/aqyaYnm

Related Article

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.