What is packing and unpacking?
Java provides the corresponding wrapper type for each basic data type
Integer i = 10;
The hand of the cigarette shook slightly: This generates an integer object with a value of 10, which automatically creates the corresponding integer object.
Name what is unpacking?
Integer i = 10; // Packing int n = i; // Unpacking
Summarize:
Boxing is to replace the automatic basic data type with the wrapper type, and unpacking is the automatic conversion of the wrapper type to the basic data type.
The following is the wrapper type for the base type:
Int (4 bytes) Integer
Byte (1 bytes) byte
Short (2 bytes) Short
Long (8 bytes) long
Float (4 bytes) float
Double (8 bytes) Double
Char (2 bytes) Character
Boolean (Undecided) Boolean
Second, the realization principle
See section Code:
1 Packagecn.zpoor.test;2 /**3 * @authorSchrödinger's Cat4 * Automatic box packing and unpacking*/5 Public classMain {6 Public Static voidMain (string[] args) {7Integer i = 10;8 intn =i;9 }Ten}
After JD Decompile:
Do not panic, calm analysis: In the case of the automatic call is the integer valueof () method, when the unboxing is automatically called the Integer Intvalue () method.
Summary: The boxing process is implemented by invoking the ValueOf method of the wrapper, which is implemented by invoking the Xxxvalue method of the wrapper (XXX represents the basic data type)
Three, the packing and unpacking the interview question
1. What is the output of this code?
1 Packagecn.zpoor.test;2 /**3 * @authorSchrödinger's Cat4 * Automatic box packing and unpacking*/5 Public classMain {6 Public Static voidMain (string[] args) {7Integer i1 = 100;8Integer i2 = 100;9Integer i3 = 200;TenInteger I4 = 200; One ASystem.out.println (I1 = =i2); -System.out.println (i3 = =I4); - } the } - - - /*Results: + * True - false + */
Why is it?
See a source:
1 Public Static Integer valueOf (int i) {2 if (i >= integercache.low && i <= Integercache.high)3 return integercache.cache[i + (-integercache.low)]; 4 return New Integer (i); 5 }
When you create an integer object by using the ValueOf method, if the value is between [-128,127], it returns a pointer to an object that already exists in Integercache.cache, or creates a new object.
So I1 and I2 are an object, and i3 and I4 point to different objects.
2. What does this code output?
1 Packagecn.zpoor.test;2 /**3 * @authorSchrödinger's Cat4 * Automatic box packing and unpacking*/5 Public classMain {6 Public Static voidMain (string[] args) {7Double I1 = 100.0;8Double i2 = 100.0;9Double i3 = 200.0;TenDouble I4 = 200.0; OneSystem.out.println (I1 = =i2); ASystem.out.println (i3 = =I4); - } - } the - - /*Results: - * False + false - */
Why is it?
Because the valueof method of the double wrapper returns a new object, each object is different. Or a word, more look at the source code.
3. What does this code output?
1 Packagecn.zpoor.test;2 /**3 * @authorSchrödinger's Cat4 * Automatic box packing and unpacking*/5 Public classMain {6 Public Static voidMain (string[] args) {7Boolean I1 =false;8Boolean i2 =false;9Boolean i3 =true;TenBoolean I4 =true; OneSystem.out.println (I1 = =i2); ASystem.out.println (i3 = =I4); - } - } the - - /*Results: - * True + * True - */
Cool Analysis:
See source
public static Boolean valueOf (Boolean b) {
return (b? True:false);
}
I say so, you probably understand.
4, talk about integer i = new integer (XXX) and integer i = XXX; The difference between the two ways?
One: The first way does not trigger the automatic boxing process; the second method triggers.
Two: The difference between execution efficiency and resource occupancy. The second approach is more efficient and resource-intensive than the first (but not absolute) situation in general.
After analysis: Hand with smoke, trembling slightly.
java-Packing and unpacking