Automatic boxing (boxing) and automatic unpacking (unboxing)
First, learn about the four classes of eight basic data types in Java
Automatic Boxing
The so-called popular point in Java is that eight basic data types are automatically changed to the corresponding wrapper type when used under certain conditions.
Listing 1 below:
@Testpublic void Boxingtest () {Integer I1 = 17;integer i2 = 17; Integer i3 = 137;integer I4 = 137; System.out.println (I1 = = I2); System.out.println (i3 = = i4);}
Output:
TrueFalse
Explain the reason for the output of true in Listing 1, clause 11:
when the wrapper type makes a "= =" comparison, i3 calls integer.valueof to automatically boxing the base data type to the wrapper type.
/*** returns an {@code integer} instance representing the specified* {@code int} value. if a new {@code integer} 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.** this method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.** @param i an {@code int} value.* @return an {@code integer} instance representing {@code i}.* @since 1.5*/public static integer valueof (int i) {if (i >= integercache.low && i <= integercache.high) return integercache.cache[i + (-integercache.low)];return new integer (i);}
As can be seen from the source code, theinteger object automatically caches an int value range in Low~high ( -128~127), which is automatically boxed as a wrapper class if this range is exceeded.
Note:
-
Integer, short, Byte, Character, long the implementation of the valueof method of the wrapper class is similar;
-
The implementation of the valueof method of Double and float is similar.
-
The implementation of the Boolean valueof method is a trinocular operation that is shaped like ' return ' (b? TRUE : FALSE); `
Automatic unpacking
The so-called pop-up in Java is: Eight wrapper types are automatically changed to the corresponding base data type when used under certain conditions.
Listing 2:
@Testpublic void Unboxingtest () {Integer I1 = 17;int i2 = 17; int i3 = 137;integer I4 = 137; System.out.println (I1 = = I2); System.out.println (i3 = = i4);}
Output:
Truetrue
Explain the reason for the output of true in Listing 2, clause 10:
When the program executes to the 10th sentence, I4 calls the Integer.intvalue method to automatically unpack the wrapper type as the base data type.
/*** Returns The value of this {@code Integer} as an* {@code int}.*/public int intvalue () {return value;}
As you can see from the source code, when the wrapper type and the base data type are compared with "= =", the wrapper type is automatically disassembled as the base data type.
Listing 3 reads as follows:
@Testpublic void Unboxingtest () {Integer I1 = 17;integer i2 = 17; Integer i3 = 137;integer I4 = 137; ==system.out.println (I1 = = I2); System.out.println (i3 = = I4); EqualsSystem.out.println (I1.equals (I2)); System.out.println (I3.equals (I4)); }
Output:
Truefalsetruetrue
Explain why the 15th sentence will output true:
Because in the Equals method implemented by the integer wrapper class, as long as the current object being compared is an integer instance, it is automatically disassembled as the base data type. The source of the Equals method from the following integer class can be seen:
/*** compares this object to the specified object. the result is* {@code true} if and only if the argument is not* {@code null} and is an {@code integer} object that* contains the same {@code int} value as this object.** @param obj the object to compare with.* @return {@code true} if the objects are the same;* {@ Code false} otherwise.*/public boolean equals (object obj) {if (obj Instanceof integer) {return value == ((Integer) obj). Intvalue ();} Return false;}
Note:
-
Integer, short, Byte, Character, long the implementation of the Intvalue method of the wrapper class is similar;
-
The implementation of the Intvalue method of Double and float is similar.
-
The implementation of the Boolean Booleanvalue method is similar to the implementation of the Intvalue method.
Packing and unpacking Consolidated list:
Public static void main (string args[]) { integer a = 1;i nteger b = 2;integer c = 3;integer d = 3;integer e = 321;Integer f = 321; Long g = 3L; long h = 2l; // will automatically unboxing (calls the Intvalue method) System.out.println (C==d);// will automatically unboxing and then automatically boxed System.out.println (e==f);// although the "= =" Comparison is whether the reference is the same object, but there are arithmetic operations, If the reference is a wrapper type, it causes the auto-unpacking System.out.println (c== (a+b));// equals to compare the contents (values) of the referenced objects for equality, but there are arithmetic operations, If the reference is a wrapper type will lead to // automatic unpacking, and then automatically boxed// a+b trigger automatic unpacking to get the value, and then automatically boxed and C comparison System.out.println (C.equals (a+b));// First A+B trigger automatic unpacking after the value of the int type, so the comparison is whether the value is equal System.out.println (g== (a+b));// first A+B trigger automatic unpacking after the value of the int type, automatically boxed after the integer type, Then G is a long type System.out.println (G.equals (a+b));// first A+H trigger automatic unpacking after the value is long, because the int type of a will automatically transition to a long type of G and then automatically boxed after the long type, // G is also a long type System.out.println (G.equals (a+h)); }
Output:
Truefalsetruetruetruefalsetrue
It is important to note that when the two operands of the "= =" operator are references to the wrapper type, it is whether the comparison points to the same object, and if one of the operands is an expression (that is, it contains arithmetic operations), the value is compared (that is, the process that triggers the automatic unpacking) . For wrapper types, the Equals method does not convert the type.
2
Java Foundation automatic packing and automatic unpacking source analysis