Beginners learn JAVA (4) ---- packing and unpacking, java ----
1. What is packing? What is unpacking?
In Java, Java provides the object package type for each basic data type. See the following table:
Packaging class table
| Basic Data Type |
Packaging |
| Byte |
Byte |
| Boolean |
Boolean |
| Short |
Short |
| Char |
Character |
| Int |
Integer |
| Long |
Long |
| Float |
Float |
| Double |
Double |
Before Java SE5, if you want to generate an Integer object with a value of 50, you need to do this:
Integer obj = new Integer(50);
In Java SE5, Java encapsulates the feature of automatic packing, and defines the value as follows:
Integer obj = 50;
In this process, an Integer object is automatically created based on the value, which is called packing;
Simply put, packing means to automatically convert the basic data type to the package type; unpacking means to automatically convert the package type to the basic data type.
So what is unpacking? And the packing object is to convert the package type to the basic data type. The disassembly and packing process is as follows:
How is the disassembly box implemented?
Taking the Double type as an example, let's take a look at the following code:
public class Main{ public static void main(String[] args){ Double DB = 10.34; double db=DB; }}
Then we decompile the code. The result is as follows:
Next, let's change it to the Integer type and try again. Let's look at the result directly:
We can see what is the same. The program automatically calls the valueof method when automatically packing, and calls the corresponding type of "**. ** Value" method when unpacking.
From this we can see that the automatic packing of a program calls the valueof method of the package type, while the unpacking is implemented by calling the "**. ** Value" method of the package.