Java automatic packing and unpacking, java packing
Java provides corresponding object types for each basic data type. Before Java SE5, if you want to generate an Integer object whose value is 7, the sample code is Integer I = new Integer (7); after Java SE5, Java provides a new syntax, this simplifies the use of basic data type objects.Autoboxing and unboxing). The previous code can be simplified to Integer I = 7 ;.
Java-provided automatic box unpacking is implemented at the compiler layer. The compiled bytecode is still in the form before Java se5. For example, automatic packing is implemented using the valueOf method of the object, and the unpacking is implemented using the xxxValue method of the object. xxx represents the corresponding basic data type.
The principle of automatic packing and unpacking is very simple. Usually, the interview will ask the object comparison questions to test the candidate's understanding of the source code.A typical case is to use = to compare whether objects are equal.In practice, the equals method of the object is usually used, which is correct. = The operation is to compare whether the object address is the same, as long as the two addresses point to different objects, = the operation will return false.
For objects of the basic data type, because some objects are frequently used, frequent object creation and collection will greatly affect the system efficiency. Therefore, the Java specification also specifies the need to cache frequently used objects.
For the Integer type, the IntegerCache class is used for caching in the specific implementation, and objects with numeric values between [-128,127] are cached. the valueOf method directly returns the object reference for the cached object, otherwise, the Integer constructor is called to create a new object. Therefore, for the number between [-128,127], true is returned when the = operation is used. Otherwise, false is returned. However, it should be noted that if the object generated by calling the constructor directly is always a new object, that is, the = operation will return false.
Similarly, Short, Long, Byte, and Integer cache the corresponding objects with values between [-128,127]. Character corresponds to the char type and has no negative number, therefore, the corresponding objects between [2, 0,127] are cached. Boolean has only two values.
As for Double and Float, they do not do any caching. The reason may be:
The number of integer values in a certain range is limited, but the floating point number is not;
It is difficult to evaluate which values are frequently used, or are not frequently used in actual use as integer values;
Based on the previous discussions, we know that you can use Integer I = new Integer (7) or Integer I = 7; To define the value type. The two methods should be used with caution:
The former will not trigger automatic packing, while the latter will trigger the automatic packing process;
The former directly generates new objects, while the latter determines whether to generate new objects or return references to cached objects based on specific values;
From the perspective of execution efficiency and resource usage, the latter is more effective and recommended;
In addition, in actual work, we stillTry to use the equals MethodI really can't think of the time to use = (except for the interview time :))