Automatic packing and unpacking.
They are the most used syntax sugar in the java language.
Start with the following code:
public static void main(String[] args) { Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; System.out.println(c == d);//true System.out.println(e == f);//false System.out.println(c == (a+b));//true System.out.println(c.equals(a+b));//true System.out.println(g == (a+b));//true System.out.println(g.equals(a+b));//false System.out.println("========================="); }
Integer a = 1;
Will be automatically boxed as follows
Integer a = Integer.valueOf(1);
Let's look at the source code of valueOf:
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
The static constants used in this code are as follows:
static final int low = -128; static final int high; static final Integer cache[]; static { int h = 127; ... high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; }
1. If two variables of Integer type are defined and their values are between [- 128128), then they will take the object in the defined cache. The packing type = = compares the address of the object, so the following returns true.
System.out.println(c == d);//true
2. For values outside the [- 128128) interval in 1, such as 321 in the example, a new object will be re created, so the following result is false.
System.out.println(e == f);//false
3. When the five operations = =, +, -, *, / are performed between the package type and the basic type, the package type will be unpacked automatically, so the following result is true.
System.out.println(c == (a+b));//true
4. Look at the equals source code of Integer as follows. If the types and values of two variables are the same, then true.
public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }
In the following code, a+b first unpacks and calculates, and then uses the c.equals(Object obj) method to convert the basic type into the encapsulation type Integer through Object. So the following is true.
System.out.println(c.equals(a+b));//true
5. g is the package type and a+b is the basic type. When the package type and the basic type are = =, they will be unpacked automatically into the basic type for comparison, so the following is true.
System.out.println(g == (a+b));//true
6. Take a look at Long's equals source code as follows. If the types and values of the two variables are the same, it will be true, otherwise it will return false.
public boolean equals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } return false; }
In the following code, a+b first unpacks and calculates, and then uses the g.equals(Object obj) method to convert the basic type into the encapsulation type Integer through Object. So as follows
System.out.println(g.equals(a+b));//false
Summary:
1. If = = of the same type is compared, if the type is referenced, pay attention to whether the two values are equal and whether the object is referenced from the cache if they are equal.
2. For +, -, *, /, = = operations, automatic unpacking will be performed.
3. equals for encapsulation type, comparison period type and value, the basic type will be automatically boxed.