The Java language Specification says that in many cases, boxing and unpacking is done by the compiler itself.
Auto-Boxing: Automatically encapsulates the base data type as an object (wrapper) type,
Auto-unpacking: Automatically re-converts objects (wrappers) into basic data types:
For example, use int for example:
Boxed integer i= 10; Equivalent to the integer i= new Integer (10);
unboxing int n = i;
The anti-compiler decompile can draw the conclusion that:
The valueof (int) method of the integer is automatically called when boxing.
(valueOf (int i) returns an Integer instance object that represents the specified int value)
The Intvalue method of integer is called automatically when unpacking.
(Intvalue () returns the Integer value as an int type)
Classic Face question 1:
Integer I1 = +; = +; = $; = $; = 100.0; = 100.0; = 200.0; = 200.0; System.out.println (i1= =i2); SYSTEM.OUT.PRINTLN (i3= =i4); System.out.println (i1= =i2); System.out.println (i3==i4);
Output result: true false false
Cause: For the first two results: The int output shows that I1 and I2 point to the same object, while i3 and I4 point to different objects. When you create an integer object by using an object in the original code and defined in the class: The ValueOf method returns a reference to an object that already exists in Integercache.cache if the value is between [-128,127], otherwise a new integer object is created. So..
For double: the number of integer values within a range is limited, and the floating-point numbers are not
Classic Face question 2:
Talk about the integer i = new Integer (1) and integer i = 1, the difference between the two ways:
1, the first way does not trigger the automatic boxing process, and the second method will trigger;
2, the difference in execution efficiency and resource occupancy. Implementation efficiency and resource occupancy in the second approach are better than the first case in general cases
Summary: The boxing process is implemented by invoking the ValueOf method of the wrapper, and the unboxing process is implemented by calling the wrapper's Intvalue method.
Note:
The difference between int and integer is the difference between the basic data type and its wrapper class: int is the basic type, the value is stored directly, the integer is the object, and the object is pointed to by a reference.
Similar to this: float float;double double;string string, and other constants and methods that are useful when working with int types
Talking about Java unpacking and packing