First of all, the basic data types in Java are really just for the convenience of programmers programming, Java itself as an object-oriented language is not the basic data type, but this will be unfriendly to the vast number of programmers, so this can also be seen as a principle to facilitate a compromise ^ ^. Ok, pull away, get to the point.
We know that the basic data types in Java (int, float, double ...) It all corresponds to a wrapper type (Integer, Float, Double ...), why is there a wrapper type? In fact, the basic data type should not exist in the pure object-oriented language Java, just for convenience, the wrapper type is the object model of the basic data type, for example:
int i1 = 10;//I1 is a variable that holds the value 10
Integer i2 = new Integer ();//new a wrapper type object, I2 is a reference to the object
Then next the Disassembly box, first of all, say I understand the disassembly box. Just two words:
1. Boxing is the conversion of the basic data type to its corresponding packaging type
2. Unpacking is to change the package type into its corresponding basic data type
Let's look at a scenario and now I'm going to store some integer data in the ArrayList, what do I do? We know that the Java Collection class is not like an array to store the base type data, and the Java Collection class can store only the object (which is actually a reference to the storage object, but generally we say the storage object).
list<int> lst = new arraylist<int> ();//This step compiles an error because the collection class cannot store the base data type
Lst.add (10);
Lst.add (23);
What about that? Is there no way to store it? In fact, since you can only store objects when the collection class cannot store data of the basic data type, we naturally think of the ability to turn data of basic data types into objects. The answer is yes, as follows:
list<integer> lst = new arraylist<integer> ();
int i1 = 10;
int i2 = 23;
Integer wrapper1 = new Integer (i1);//Change the base data type I1 to an object Wrapper1 (this is called Manual boxing)
Integer wrapper2 = integer.valueof (I2);
Lst.add (Wrapper1);
Lst.add (WRAPPER2);
Before java5.0, we had to store the base data type in the collection class in the same way as above, but after java5.0, Java supported the automatic unpacking box, as follows:
list<integer> lst = new arraylist<integer> ();
int i1 = 10;
int i2 = 23;
Integer wrapper1 = i1;//Automatic boxing of I1
Integer wrapper2 = i2;//Automatic boxing of I2
Lst.add (Wrapper1);
Lst.add (WRAPPER2);
int i3 = wrapper1;//Automatic unpacking
As you can see here, we don't need to pass the basic data variable as a parameter to the constructor of the wrapper type to new a wrapper type variable, but instead direct the base data type's face-changing amplitude to a wrapper type reference. In fact, this is the Java automatic packing for us.
In addition, in the operation of the time can also be automated disassembly box, as follows:
Integer i = 10;//Auto-boxing
SYSTEM.OUT.PRINTLN (i + 20);//This is the first unboxing of I into the basic data type, and then the addition operation
Add two points:
1. Auto-boxing is actually called the Integer.valueof () method
2. Auto-boxing is actually a kind of compiler honey (Compiler sugar, the compiler is deceiving us, let us feel this API good use, very concise, in fact, it is in the compile time or pretend to replace the "stupid way", as if to take the honey tease us to play), as follows:
Integer i = 100;
The actual JDK automatically compiles the above code into the following form:
Integer i = integer.valueof (100);
Java Automatic Unboxing understanding