Automatic packing and unpacking

Source: Internet
Author: User

1. Packing
    • Boxing: Basic Type--reference data type
int num = 20;integer number = num;  Automatic Boxing

The last statement above corresponds to the following: integer number = new integer (num);


  • But here's the problem.
  • Double d1=1.0;double D2=new Double (1.0);

    Double D2 = new double (d1) and integer number = new integer (num); Is it a concept?

  • The Integer wrapper class implements the constant pool technique, so num 's is obtained from the constant pool

  • a double does not implement constant pool technology, and instead of looking for data from a constant pool, it is a new object from the heap. That is, 1.0 is in the heap, not in a constant pool.

2. Unpacking
    • Unboxing: Reference data Type-Basic type
Integer number = new integer, int num = number;  Automatic unpacking
The last statement above is equivalent to: int num = Number.intvalue ();


3. Example 1 3.1 Code

Package com.ilaoda.day0905;/** * Automatic packing  unpacking * @author Administrator * */public class Test3 {public static void main (Strin G[] args {integer in1 = new Integer, Integer in2 = new Integer, integer in3 = 100;integer in4 = 100; System.out.println ("in1 = = in2:" + (in1 = = in2));  FalseSystem.out.println ("in1 = = in3:" + (in1 = = in3));  FalseSystem.out.println ("in3 = = In4:" + (in3 = = in4));  True  automatically boxed, the value is within the byte range, so pointing to the same area/** * Why the comparison with the above in3, Int4 is true. But in5 = =  in6 will appear false? */integer In5 = 128;integer in6 = 128; System.out.println ("In5 = = in6:" + (In5 = = in6));  False}}
3.2 Answers
in1 = = In2:falsein1 = = In3:falsein3 = = In4:truein5 = In6:false
3.3 Explanation

Here's a little detail to note:
For an auto-boxing operation, if the value is assigned in the byte range, that is, -128~127, they point to the same heap space area. if the range of byte is exceeded, then each creates its own object, which is a new space to be re-created.


4. Example 24.1 Code


Package Com.ilaoda.day0905;public class Test4 {public      static void Main (string[] args) {              objpooltest ();      } Public        static void Objpooltest () {          int i = +;          int i0 = +;          Integer i1 = +;          Integer i2 = +;          Integer i3 = 0;          Integer i4 = new Integer (+);          Integer i5 = new Integer (+);          Integer I6 = new Integer (0);          Double d1=1.0;          Double d2=1.0;                    System.out.println ("I=i0:" + (i = = I0));         System.out.println ("I1=i2:" + (I1 = = I2));         System.out.println ("I1=i2+i3:" + (I1 = = i2 + i3));          System.out.println ("I4=i5:" + (I4 = = i5));         System.out.println ("I4=i5+i6:" + (I4 = = i5 + I6));         System.out.println ("D1=D2:" + (D1==D2));                  System.out.println ();       }  }  
4.2 Answers
I=i0:truei1=i2:truei1=i2+i3:truei4=i5:falsei4=i5+i6:trued1=d2:false



4.3 explanation



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.