Defined
In Java, data types can be divided into two categories, the basic data type and the reference data type, the data of the base data type is not an object, so Java provides the corresponding wrapper class for the data type to use as an object. (For a detailed description of the packaging class, please refer to the blog Java Basics of the common class)
This blog mainly describes the packaging of the automatic packing and unpacking mechanism.
Boxing is the use of basic data types wrapped in their corresponding reference types, so that they can have the characteristics of the object, such as we can wrap the int type into an integer type of object, or a double packing scale double, and so on.
The so-called unboxing, is the opposite of boxing, the integer is a double, such as a reference type of object re-simplified to the basic data type of data.
The automatic boxing and unboxing introduced by the Java JDK is the compiler that decides whether to do boxing or unboxing according to the code we write. That is, you do not need to use methods such as valueof () and Intvalue (). (If this is not familiar, please refer to the blog Java Basics of the common class of the detailed)
Considerations for the automatic packing and unpacking mechanism
This is the focus of this blog to tell
Let's look at a small example:
Public classWrappertext { Public Static void Main(string[] args) {Integer A = -; Integer B = -; Integer C = $; Integer d = $; System. out. println (A = = B);//1System. out. println (A = = -);//2System. out. println (c = = d);//3System. out. println (c = = $);//4}}
If we don't know enough about the automatic boxing and unpacking operations in Java, we will think: after auto boxing, ABCD is a different reference to an integer object, and the "= =" operator is used to compare object references, so both 1 and 3 should output false. for 2 and 4, because both A and C are automatically disassembled, their comparison is a comparison of the basic data types. the "= =" operator compares the values so that they are 2 and 4 because the output is true. That is, the output of the final result:
False
True
False
True
But the right results are:
We see that the result of 1 for true,3 is false, which is why. In fact, it is very simple, the automatic boxing process is actually called valueof () method, and the valueof () method of the source code is:
publicstaticvalueOf(int i) { assert127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; returnnew Integer(i); }
Private Static class integercache { Static Final intLow =- -;Static Final intHighStatic FinalInteger cache[];Static{//High value is configured by property inth =127; String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");if(Integercachehighpropvalue! =NULL) {inti = parseint (Integercachehighpropvalue); i = Math.max (i,127);//Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value-(-low)-1); } high = h; Cache =Newinteger[(high-low) +1];intj = Low; for(intK =0; K < Cache.length; k++) Cache[k] =NewInteger (j + +); }Private Integercache() {} }
By reading the source code, you can find that within Java, in order to save memory, an array in the Integercache class caches an integer object with values from 128 to 127. When we call integer.valueof (int i), if the value of I is between 128 and 127, an object is returned directly from the cache, otherwise a new integer object. Therefore, 1 is true,3 to false.
Finally, share a very interesting topic related to it:
Please provide a declaration of I that transforms the following loop into an infinite loop:
while(jiijij){ }
I believe if you read this blog carefully, this problem will be able to easily solve.
Automatic packing and unpacking of Java-based packaging class