Boxing and unboxing in Java

Source: Internet
Author: User

Test code:

System.out.println (0L = = 0);//true
System.out.println (((Long) 0L). Equals (0));//false
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 1000;//if the int values between-128 and 127,VM use the same object, the new object is created.
Integer i4 = 1000;
System.out.println (I1==I2);//true
System.out.println (I3==I4);//false

Autoboxing and unboxing also known as unpacking and boxing, simply speaking, is to convert from primitive to wrapper class, such as int type to integer type is boxing, and the integer type to int type is unboxing. Of course, the boxing and unpacking here is auto, is the JVM in the work of the content, in fact, we do not hand-written, but also has a handwritten counterpart, as follows:

1 int i=10;
2 integer a=new integer (i);//Boxed operation
3 int j=a.intvalue ();//unboxing operation

The above is manual and has been automatically boxed and unboxing converted in the JVM after Java5.0, as shown below:

1 int i=10;
2 Integer b=i;//Automatic Boxing
3 int k=b;//Automatic unpacking

Boxing and unpacking is so simple, the following can see how the self-increment is a process, this is a very interesting thing, diminishing is the same.

1 integer d=new integer (10);
2 d++;//This statement causes D to remove the box first, then the + + operation, and then the result is re-boxed

The above statement allows Java to ensure that the wrapper class can also be used normally as a generic operator, but this is definitely not an operator overload in C + +. You can also try to analyze the packing and unpacking process in ternary and conditional expressions.

One thing to note here is the problem of boxing and unpacking in method overload, such as the following:

1 public void dosomething (double num);
2 public void dosomething (Integer num);

There are two functions in a class, and for integer int k, which is called if dosomething (k) is called?

In Java1.4, it is obvious that a function called a double type, however, now has the automatic unpacking and boxing, which will be called after? Also a double type of function. This will ensure that the previous code will run correctly in the new version as well. ( backward compatible, backwards compatibility, the computer in a program or class library to update to a newer version, the old version of the program created by the document or system can still be normal operation or use, Or a program developed on the basis of an older version of the class library can still compile and run normally. )

Note that in Java5, method parsing is the process of three pass:

A. The compiler will try to locate the correct method without any boxing and unboxing, or enable VARARG (variable parameters). This will find any method that will be called according to the rules of Java1.4.

B. If the first pass fails, the compiler will attempt to parse the method again, but this time it will allow boxing and unboxing to be converted. The method with vararg is not in the consideration of this pass.

C. If the second pass fails, the compiler will make the last attempt to allow boxing and unboxing, and also take into account the Vararg method.

Boxing and unboxing in Java (GO)

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.