I. Preface
Java automatic packing and automatic unpacking are new features provided by JDK, so they can be used in Versions later than JDK. These features are not supported in previous versions.
To understand automatic packing and automatic unpacking, you must first understand the eight primitive data types and corresponding encapsulation classes in java.
Ii. Eight original data types and their corresponding encapsulation classes
---> ---> ---> ---> ---> ---> ---> ---> Boolean
Note: The encapsulation classes of the raw data type are all under the java. lang Package and can be directly used.
Iii. What is automatic packing?
Automatic Packing means that an original data type can be directly assigned to the corresponding encapsulation class. The type conversion is automatically handled by the java compiler, for example:
Byte B = 1 = 2000 = 300000 = 2.5f = 2.555 = '中' =;
If you want to create an original data-type encapsulation class object without automatic packing (JDK5.0 or earlier), you need to change it to the following format (taking Integer as an example ):
= Integer(1= Integer("1"= Integer.valueOf(1);
Note: automatic packing can be simply understood as the original data type can be automatically loaded into the encapsulation class.
4. What is automatic unpacking?
Automatic unpacking means that the encapsulation Class Object of the original data type can be processed as the original data type, and the conversion is automatically processed by the java compiler, for example:
Integer I = 2000 = 'medium 'value = I + c + 100;
Before automatic unpacking (the version before JDK5.0), the above operations need to be changed to the following form:
'Value = I. intValue () + c. charValue () + 2000;
Note: The encapsulation class of the original data type can be converted to the original data type automatically.
V. Benefits of automatic packing and unpacking
In actual development, we often operate on the encapsulation class of the original data type, and often need to convert or perform some operations on the original data type and encapsulation class,
These operations are cumbersome if they are not automatically packed or unpacked. So the benefits of automatic packing and automatic unpacking are obvious.
6. Pay attention to the use of automatic packing and automatic unpacking. The following two cases are described (common during interviews)
(1) read the following code and write the output result.
Integer i = 100= 100=== 200= 200== k = 100==
(2) read the following code and write the output result.
Integer i = 100= Integer(100= Integer.valueOf(100==== k);
(3) is the following code correct? If yes, what is the output result?
Integer i = 100 j = 100= 100==== k);
Case Analysis (1 ):
The output result is:
truetruefalsetruetruetrue
There are two key points to understanding this question:
1. "=" The comparison operator compares the referenced addresses, and The equals () method compares the content (Note: You can override the equals () method to customize the comparison rules)
2. When the original data type is automatically boxed as an encapsulation object, if the value of the original data type is-128 ~ 127 (equivalent to the byte value range), the java compiler will determine whether the same conversion has been performed. If so, the reference address of the previously loaded encapsulated class object will be directly assigned to the current object, therefore, in Integer I = 100 and Integer j = 100, I shares an Integer object with j.
Case Analysis (2 ):
The output result is:
falsefalse
Cause: new Integer (100) and Integer. valueOf (1) are all new objects, So I, j, k are three different objects.
Case Analysis (3)
Code error. Change Long k = 100 to Long k = (long) k;