Explanation of Java constant pool: An interview question that is quite cool

Source: Internet
Author: User
Today, my collegou asked me a Java question that gave him a headache.

CodeAs follows:

This Article From dreamsea (Zhang xiaozhe). For more information, see the source!
 /**  
*
*
@ Author Dreamsea 2011-11-19
*/
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 ();
}
}

It seems easy, but the result output by the console is exactly the opposite of what we think. We are confused. Why? [Display output result]
I1 = I2 true
I1 = I2 + I3 true
I4 = I5 false
I4 = I5 + I6 true
Finally, I learned through online search that Java provides String Class-like object pool mechanism, of course, Java's eight Basic Type Packaging class ( Packaging type ). 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
* Shoshould 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(IntI = 0; I <cache. length; I ++)
Cache [I] =NewINTEGER (I-128 );
}

From this static block, we can see that integer has created a value by default 【-128-127. 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.

For I1 = I2 + I3, I4 = I5 + I6, the result is true because,Java's mathematical computing is operated in the memory stack.Java will perform the binning operation on 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 here should have a better understanding of the integer Object pool, I asked novogene, "if I changed 40 to 400, I guess what would be output? [Display output result] I1 = I2 false
I1 = I2 + I3 true
I4 = I5 false
I4 = 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 references.

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.