Java automatic packing and unpacking Summary

Source: Internet
Author: User

Java automatic packing and unpacking Summary

I used to know a little about the automatic packing and unpacking introduced by Java. I recently read a blog and found that I know too little about the feature of automatic packing and unpacking, so I have studied this feature today. The following is a summary of the test code.

Test code:
        int a = 1;        Integer b = 1;        Integer c = 1;        Integer d = 2;        Integer e = 3;        Integer f = 128;        Integer g = 128;        Long h = 3L;        Double m = 4.0;        Double n = 4.0;        Float p = 5f;        Float q = 5f;        System.out.println("a == b : " + (a == b));  //true        System.out.println("b ==c : " + (b == c));  //true        System.out.println("e == (c + d) : " + (e == (c + d)));  //true        System.out.println("e.equals(c + d) : " + (e.equals(c + d)));  //true        System.out.println("h == (c + d) : " + (h == (c + d))); //true        System.out.println("h.equals(c + d) : " + (h.equals(c + d)));  //false        System.out.println("f == g : " + (f == g)); //false        System.out.println("m == n : " + (m == n)); //false        System.out.println("p == q : " + (p == q)); //false        System.out.println("m == d * 2 : " + (m == d * 2)); //true        System.out.println("p == (d + e) : " + (p == (d + e))); //true

Test output result and description:

1. a = B: true when the basic type package class and the basic type value are =, the package class is automatically split. That is, the basic type value is compared. The specific implementation is to call the Integer. intValue () method to implement unpacking. You can open a breakpoint in the test code and use the F5 shortcut to step into to each execution method. We can see that the Unbox is implemented by calling the Integer. intValue () method. 2. B = c: true B and c are both Integer packaging classes, so the basic types are automatically boxed and assigned to B and c. The = operation does not trigger the binning operation. Therefore, the reference address is compared, indicating that B and c are the same object. In java, the automatic packing call is implemented by the Integer. valueOf () method, and breakpoint verification can be performed like in Example 1. Why is B and c the same object? View the implementation of the Integer. valueOf () method, as shown in the following code: for the values of-128 to 127, the value in IntegerCache is obtained directly. IntegerCache is a static internal class in Integer, which will be-128 to 127 (that is, all signed values-2 ^ 7 to 2 ^ 7-1 that a byte can represent) the package class exists in an array. For the number between-128 and 127, it is obtained directly from the array, and the other number is generated using new. Therefore, true is output here. Public static Integer valueOf (int I) {if (I> =-128 & I <= IntegerCache. high) return IntegerCache. cache [I + 128]; else return new Integer (I);} 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 (integerCacheHighPropValue! = 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 ++);} 3. e = (c + d): true the package class will trigger the binning operation when performing addition, subtraction, multiplication, division, and remainder operations. Therefore, c and d are added after unpacking and the result is of the basic type; then perform the = operation on e and the basic type to trigger the binning operation. 4. e. equals (c + d): true first, the c + d binning operation obtains the basic type value. Then, when the equals operation is performed, the binning operation of the basic type value is triggered, the result of c + d is automatically packed as a packaging class. Finally, perform the equals operation with e. 5. h = (c + d): the true operation sequence is the same as that in the preceding example 3. The only difference is that h automatically unboxing calls the Long. longValue () implementation. 6. h. equals (c + d): the operation order of "false" is the same as that in the preceding example 4. The reason for "false" is that the computation result of "c + d" is automatically boxed and the type is Integer, the h type is Long and the type is different. The equals result is false. 7. f = g: false refer to the above example 2. The cache range from-128 to 127 is exceeded. Therefore, the valueOf () method uses new to generate a new object. 8. m = n: false p = q: false is different from the result in Example 1 and 2 above. For Boolean, Byte, Character, Short, Integer, and Long, the cache mechanism is implemented for values within one byte-128 to 127 (Boolean is only true and false. A new object is generated in the corresponding valueOf () method only when the number is not in this range. However, for floating point data of the Double and Float types, there are countless Decimals in addition to the 128 integers between-127 and 256. Therefore, java does not implement caching of some numbers in the Double and Float types. Therefore, automatic packing of Double and Float is a new object. Therefore, both cases output false. 10. m = d * 2: true p = (d + e): true. See example 3 and Example 5.
Decompiled code:

Use the java decompilation tool to decompile the class bytecode file. The result is as follows. From the decompilation code below, we can see how java implements automatic packing and unpacking.

    int a = 1;    Integer b = Integer.valueOf(1);    Integer c = Integer.valueOf(1);    Integer d = Integer.valueOf(2);    Integer e = Integer.valueOf(3);    Integer f = Integer.valueOf(128);    Integer g = Integer.valueOf(128);    Long h = Long.valueOf(3L);    Double m = Double.valueOf(4.0D);    Double n = Double.valueOf(4.0D);    Float p = Float.valueOf(5.0F);    Float q = Float.valueOf(5.0F);    System.out.println("a == b : " + (a == b.intValue()));    System.out.println("b ==c : " + (b == c));    System.out.println("e == (c + d) : " + (e.intValue() == c.intValue() + d.intValue()));    System.out.println("e.equals(c + d) : " + e.equals(Integer.valueOf(c.intValue() + d.intValue())));    System.out.println("h == (c + d) : " + (h.longValue() == c.intValue() + d.intValue()));    System.out.println("h.equals(c + d) : " + h.equals(Integer.valueOf(c.intValue() + d.intValue())));    System.out.println("f == g : " + (f == g));    System.out.println("m == n : " + (m == n));    System.out.println("p == q : " + (p == q));    System.out.println("m == d * 2 : " + (m.doubleValue() == d.intValue() * 2));    System.out.println("p == (d + e) : " + (p.floatValue() == d.intValue() + e.intValue()));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.