Summary of Java automatic packing and unpacking

Source: Internet
Author: User

For the java1.5 introduction of the automatic packing unpacking, just know a little bit, recently when reading a blog, I found that the automatic packing unpacking this feature too little understanding, so today to study the next feature. The following is a summary of the combined test code.

Test code:
int a =1;Integer B =1;Integer C =1;Integer d =2;Integer e =3;Integer f = -;Integer g = -;Long h =3L;Double m =4.0;Double n =4.0;Float p =5F;Float q =5F;System. out. println("A = = B:"+ (A = = b)); TrueSystem. out. println("b ==c:"+ (b = = c)); TrueSystem. out. println("E = = (c + d):"+ (E = = (c + d))); TrueSystem. out. println("E.equals (c + D):"+ (E. Equals(c + D))); TrueSystem. out. println("H = = (c + d):"+ (H = = (c + d)));//trueSystem. out. println("H.equals (c + D):"+ (H. Equals(c + D))); FalseSystem. out. println("F = = G:"+ (f = = g));//falseSystem. out. println("M = = N:"+ (M = = N));//falseSystem. out. println("p = = Q:"+ (P = = q));//falseSystem. out. println("M = = d * 2:"+ (M = = d *2));//trueSystem. out. println("p = = (d + e):"+ (P = = (d + e)));//true

Test output results and Description:

 1.A = = B:trueWhen the basic type wrapper class and the base type value are in the = = operation, the wrapper class is automatically unboxing.     That is, the base type value is compared.     Specifically implemented, the Integer.intvalue () method is called to implement the unboxing. You can break the point at the test code, use the F5 shortcut key, step into to each step, and see that the Integer.intvalue () method is called to implement the unboxing.2.b = = C:trueThe B and C types are integer wrapper classes, so the basic types are automatically boxed and assigned to B and C. Unpacking is not triggered when the = = operation is performed.    So the comparison is the reference address, stating that B and C are the same object. The automatic boxing call in Java is implemented by the Integer.valueof () method, which can be used as an example1Same as interrupt point verification. Why would B and C be the same object? See the Integer.valueof () method implementation to know, as follows: As you can see in the following code, for- -To127This theA value that is directly obtained from the Integercache. And Integercache is a static inner class in the integer that will- -To127(That is, all the signed values that a byte can represent-2^7To2^7-1), the wrapper class exists in an array. For -To127The number is obtained directly from the array, and the other numbers are usedNewGenerated. So the output heretrue。 Public StaticIntegervalueOf(inti) {if(I >=- -&& I <= Integercache.high)returnIntegercache.cache[i + -];Else            return NewInteger (i); }Private Static  class integercache {        Static Final intHighStatic FinalInteger cache[];Static{Final intLow =- -;//High value is configured by property            inth =127;if(Integercachehighpropvalue! =NULL) {//Use Long.decode This avoid invoking methods that                //Require Integer ' s autoboxing cache to be initialized                inti = Long.decode (integercachehighpropvalue). Intvalue (); i = Math.max (i,127);//Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value--low);            } high = h; Cache =Newinteger[(high-low) +1];intj = Low; for(intK =0; K < Cache.length; k++) Cache[k] =NewInteger (j + +); }3.E = = (c + d):truePackaging class in the execution of subtraction, and other operations, will trigger the unpacking operation, so C, D after unpacking, the result is the basic type; then E and the basic type = = operation, triggering the unboxing operation.4.E.equals (c + D):trueFirst, the C + D unpacking operation gets the base type value, and then, when the equals operation is performed, the boxing operation of the base type value is triggered, and the result of C + D is automatically boxed into the wrapper class, and the last equals operation with E.5.h = = (c + d):trueThe order of operations and the example above3The only difference is that H auto-unpacking is called the Long.longvalue () implementation.6.H.equals (c + D):falseThe order of operations and the example above4In the same, forfalseThe reason is that the result of the operation of C + D is automatically boxed after the type is integer, and h is of type long, the type is different, and the result of equals isfalse。7.f = = g:falsePlease refer to the example above2explained in the. Beyond the- -To127ValueOf () method is used in theNewA new object is generated.8.m = = N:falsep = = Q:falseWith the above example1、2, for Boolean, Byte, Character, short, Integer, long six basic types, for values within one byte- -To127(Boolean onlytrueAndfalse) implements the caching mechanism. The number in this range is not in the corresponding valueof () methodNewOut a new object. However, for floating-point data of double and float types, the- -To127Among other than theThere are countless decimals in addition to integers, so there is no cache in Java for some number in double and float. Therefore, the automatic boxing of double and float isNewOut of the new object. Therefore, both cases are outputfalse。.m = = d *2:truep = = (d + e):truePlease refer to the example3and examples5。
Post-compilation Code:

Using the Java Anti-compilation tool, the class bytecode file is deserialized and the results are as follows. From the following anti-compilation code, we can see how the Java implementation of automatic boxing, 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( -);Integer g = integer. ValueOf( -);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()));

Reference article:
①java Automatic box packing and unpacking
②java automatic packing and unpacking and its traps
③ in-depth analysis of boxing and unpacking in Java
④ Four Java basic questions how many can you have on the road?

Summary of Java automatic packing and unpacking

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.