Constant pools for Java wrapper classes

Source: Internet
Author: User

Integer a=integer.valueof (100);
Integer b=integer.valueof (100);
System.out.println (A==B);
Double d1=double.valueof (100);
Double d2=double.valueof (100);
System.out.println (D1==D2); Why is it that the wrapper class Ingeter two values equal to a double is not equal?

When assigning a value to an integer, it is actually an auto-boxing process, which is called the integer.valueof (int) method, which uses a constant pool when the value is greater than or equal to 128 and less than or equal to 127, so the first two addresses are equal, but the last two are more than 127, Therefore, a constant pool is not used.

Other words
Integer-128~127 actually you can think of as an int, so the output of the first class should be = =
Interger more than 128 of the value can not be regarded as int, he is the object, two different values of the same object if it is determined to be unequal, can be judged by equals.

8 Basic types of Java (Byte, short, Integer, Long, Character, Boolean, Float, double), except for Float and double, the other six implements a constant pool, However, they use constant pools only when they are greater than or equal to 128 and less than or equal to 127.Java wrapper class constant pool detailed

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);
Integer i7 = 140;
Integer i8 = 140;
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 ("i7=i8\t" + (i7 = = i8);
System.out.println ();
}
}

I1=i2 true
I1=i2+i3 true
I4=i5 false
I4=i5+i6 true

I7=i8 false

Java provides the same object pooling mechanism as the string class in order to improve performance, and of course Java's eight basic types of wrapper classes (packaging type) also have an object pooling 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:

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.

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=i2false i1=i2+i3true I4=i5false I4=i5+i6true

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.

Constant pools for Java wrapper classes

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.