Automatic packing and unpacking are a common issue in Java. Today we will take a look at some of the problems in packing and unpacking. This article first describes the most basic items of packing and unpacking, and then let's take a look at the problems frequently encountered in the interview test. The following is the directory outline of this article: 1. What is packing? What is unpacking? Ii. How to Implement packing and unpacking 3. If any errors occur during the interview, please forgive me and criticize and correct me. I am very grateful. 1. What is packing? What is unpacking? As mentioned in the previous article, Java provides the corresponding package type for each basic data type. I will not elaborate on why the package type is provided for each basic data type, if you are interested, you can view the relevant information. Before Java SE5, if you want to generate an Integer object with a value of 10, you must do this: Integer I = new Integer (10 ); in Java SE5, automatic packing is provided. to generate an Integer object with a value of 10, you only need to: Integer I = 10; in this process, the corresponding Integer object is automatically created based on the value, which is the packing. So what is unpacking? As the name implies, the package type is automatically converted to the basic data type: 12 Integer I = 10; // boxed int n = I; // 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. The following table lists the types of wrapper corresponding to the basic data type: int (4 bytes) Integerbyte (1 byte) Byteshort (2 bytes) bytes long (8 bytes) Longfloat (4 bytes) floatdouble (8 bytes) Doublechar (2 bytes) Characterboolean (undetermined) Boolean 2. how to Implement packing and unpacking: After learning about the basic concepts of packing in the previous section, this section will show you how to implement packing and unpacking. Take the Interger class as an example. Let's take a look at the following code: public class Main {public static void main (String [] args) {Integer I = 10; int n = I ;}} after the class file is decompiled, the following content is obtained: From the byte code obtained by decompiling, the valueOf (int) method of Integer is automatically called during packing. The intValue method of Integer is automatically called during unpacking. Others are similar, such as Double and Character. If you don't believe it, you can try it manually. Therefore, we can summarize the implementation process of packing and unpacking in one sentence: the packing process is implemented by calling the valueOf method of the package, and the unpacking process is implemented by calling the xxxValue method of the package. (Xxx indicates the corresponding basic data type ). Iii. Questions Related to the interview although most people have a clear understanding of the concepts of packing and unpacking, but the questions related to packing and unpacking in the interview and written examination may not be answered. The following lists some common interview questions related to packing/unpacking. 1. What is the output result of the following code? Public class Main {public static void main (String [] args) {Integer i1 = 100; Integer i2 = 100; Integer i3 = 200; Integer i4 = 200; System. out. println (i1 = i2); System. out. println (i3 = i4) ;}} some friends may say that the output is false, or some may say that the output is true. But in fact, the output result is: why is such a result? The output results show that i1 and i2 point to the same object, while i3 and i4 point to different objects. At this point, you only need to check the source code. The following code is the specific implementation of the Integer valueOf method: public static Integer valueOf (int I) {if (I >=- 128 & I <= IntegerCache. high) return IntegerCache. cache [I + 128]; else return new Integer (I);} AND THE IMPLEMENTATION OF THE IntegerCache class is: copy the code private static class IntegerCache {static final int high; static final Integer cache []; static {final int low =-128; // high value may be configured by property int h = 127; if (inte GerCacheHighPropValue! = Null) {// Use Long. decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int I = Long. decode (integerCacheHighPropValue ). intValue (); I = Math. max (I, 127); // Maximum array size is Integer. MAX_VALUE h = Math. min (I, Integer. MAX_VALUE--low);} high = h; cache = new Integer [(high-low) + 1]; int j = low; for (int k = 0; k <cache. length; k ++) cache [k] = New Integer (j ++);} private IntegerCache () {}} copy the code from the two sections of code, we can see that when creating an Integer object using the valueOf method, if the value is between [-128,127], it is directed to IntegerCache. references of existing objects in the cache; otherwise, a new Integer object is created. In the above Code, The i1 and i2 values are 100, so the existing objects are directly retrieved from the cache. Therefore, i1 and i2 point to the same object, i3 and i4 point to different objects respectively. 2. What is the output result of the following code? Public class Main {public static void main (String [] args) {Double i1 = 100.0; Double i2 = 100.0; Double i3 = 200.0; Double i4 = 200.0; System. out. println (i1 = i2); System. out. println (i3 = i4) ;}} some friends may think that the output result is the same as that of the above question, but it is actually not. The actual output result is: falsefalse. For the specific reason, you can view the implementation of the valueOf Double class. Here we will only explain why the valueOf method of the Double class is implemented differently from the valueOf method of the Integer class. Very simple: the number of integer values in a certain range is limited, but the number of floating points is not. Note that the implementation of the valueOf methods of Integer, Short, Byte, Character, and Long classes is similar. The implementation of the valueOf Method of Double and Float is similar. 3. what is the output result of the following code: 1 public class Main {public static void main (String [] args) {Boolean i1 = false; Boolean i2 = false; Boolean i3 = true; boolean i4 = true; System. out. println (i1 = i2); System. out. println (i3 = i4) ;}} as to why this result is returned, the source code of the Boolean class is also clear at a glance. The following is the implementation of the Boolean valueOf method: public static Boolean valueOf (boolean B) {return (B? TRUE: FALSE);} What are TRUE and FALSE? Two static member attributes are defined in Boolean: copy the code public static final Boolean TRUE = new Boolean (true ); /*** The <code> Boolean </code> object corresponding to the primitive * value <code> false </code>. */public static final Boolean FALSE = new Boolean (false); copy the code so far, you should understand why the output above is true. 4. Let's talk about the differences between Integer I = new Integer (xxx) and Integer I = xxx. Of course, this question is of a broad type. But the key points must be answered. I will summarize the two main differences: 1) the first method will not trigger the automatic packing process; the second method will trigger; 2) the difference between execution efficiency and resource usage. The execution efficiency and resource occupation in the second method are superior to that in the first case in general cases (note that this is not absolute ). If any of your friends has additional content, please leave a message below. Thank you very much.