Java provides the corresponding object type for each of the basic data types. Before Java SE5, if you were to generate an integer object with a numeric value of 7, the code example: integer i = new integer (7); Java SE5, Java provides a new syntax that simplifies the use of basic data type objects, which we call Automatic boxing (autoboxing) and unpacking (unboxing). The previous code can be simplified to integer i = 7;.
The Automatic packing box unpacking provided by Java is implemented at the compiler level. The compiled bytecode is still in the form before Java SE5. such as automatic boxing using the object's ValueOf method implemented, and the unboxing using the object's Xxxvalue method, XXX represents the corresponding basic data type.
The principle of automatic boxing and unpacking is very simple, usually the interview will ask the question of the comparison, in order to examine the candidate's understanding of the source code. A more typical case is to use = = to compare objects for equality. It is not a problem to use the Equals method of the object in the actual work. The = = operation is to compare the address of the object is the same, as long as two addresses point to different objects, = = operation will return FALSE.
For objects of the basic data type, due to the fact that some objects are often used, if the object is created and recycled frequently, the system efficiency will be greatly affected. The Java specification therefore also requires caching of frequently used objects.
For an integer type, the implementation uses the Integercache class for caching, caches the value between [-128,127] objects, and the ValueOf method returns the object reference directly to the cached object, otherwise the integer constructor is called to create the new object. So the auto-boxing will return true for the number between [-128,127], using the = = operation, and the other case is false. However, it is important to note that if the object generated by the constructor method is called directly, it will always be a new object, that is, the = = operation will return FALSE.
Similar implementations, short, Long, Byte, Integer cache the corresponding object of the value between [-128,127]; character corresponds to char type, there is no negative number, so the corresponding object between [0,127] is cached The Boolean has only two values and is cached.
As for double and float, they did not do any caching. The reasons may be:
-
Since the number of integer values in a range is limited, the floating-point numbers are not;
-
It is difficult to assess which numbers are often used, or which are not commonly used in the process of actual use, as in the case of integer values;
Based on the previous discussion, we know that defining a numeric type can use an integer i = new Integer (7), or an integer i = 7;
-
The former does not trigger automatic boxing, the latter will trigger the automatic boxing process;
-
The former generates a new object directly, and the latter determines whether to generate a new object or to return a reference to the cached object, depending on the specific value;
-
From the implementation of efficiency and resource consumption, the latter is more effective, recommended use;
In addition, in the actual work, we still try to use the Equals method , it is not possible to think of when will use = = Operation AH (except for the interview:))
Java Automatic Boxing and unpacking