A little understanding of the Java constant pool

Source: Internet
Author: User

About a face question on the net,Output Results:
  
 
  1. /**
  2.  * 
  3. * @author Dreamsea 2011-11-19
  4.  */  
  5. Public class integertest {
  6. Public static void main (string[] args) {
  7. Objpooltest ();
  8. }
  9. Public static void objpooltest () {
  10. Integer I1 = +;
  11. Integer i2 = +;
  12. Integer i3 = 0;
  13. Integer i4 = new integer (+);
  14. Integer i5 = new integer (+);
  15. Integer I6 = new integer (0);
  16. System.out.println ("i1=i2\t" + (I1 = = I2));
  17. System.out.println ("i1=i2+i3\t" + (I1 = = i2 + i3));
  18. System.out.println ("i4=i5\t" + (i4 = = i5));
  19. System.out.println ("i4=i5+i6\t" + (I4 = = i5 + I6));
  20. System.out.println ();
  21. }
  22. }
Results of the output
I1=i2truei1=i2+i3  Truei4=i5falsei4=i5+i6true
Some understanding: First what is a constant pool? According to Baidu's answer, the constant pool is a special storage space in the JVM. Used to store classes, interfaces, constants in methods, and some string constants, and of course, you can also extend the constants produced by the executor into a constant pool. It has been determined at compile time that it is part of the existing. class file. In other words, when the program is not dynamically input data, it already exists, which greatly improves the efficiency of the program operation.
Talk about the reasons for the above questions and integer first = = on both sides if it is an object, the comparison is a reference to the two object is the address, not its value. I4 and i5 are two different objects so the output falsei1 is equal to I2, because there is a block of code in the integer for the constant pool, storing the constant value between the -128~127, so each number between this does not open an additional address, that is, I1 and I2 are the same object, so the output True the second I1==i2+i3 why the return is true, that is because Java has the characteristics of unpacking, I1,i2,i3 is originally an object, but in the operation is automatically converted to int type and then compare, this time = = on both sides of the comparison is the operation value, so equal,The fourth one is also the same principle.
so think about if it is an Integer i1 = 128; Integer i2 = 128; What will be the result of System.out.println (I1==I2)? Yes, the program output is false, because 128 is not in the constant pool of integers.
Let's talk a little more about string's constant pool string s1 = "Hello"; string s2 = "Hello" string s3 = new String ("Hello");System.out.println (S1 ==s2); System.out.println (S1 ==s3);  What would be the result? Answer:truefalsethe string created with the new string () is an object that is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its own address space. OK, one more.
string s4 = "he" +new string ("Llo");System.out.println (S1 = = S4);
The result? The answer is false, so as long as it is a new object in string, two objects are not equal and the result must be false.






















A little understanding of the Java constant pool

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.