Java Fundamentals: New features after JDK1.5: Automatic disassembly box, and precautions

Source: Internet
Author: User

First look at a piece of code:

1         New Integer (4); 2         Integer y = 4;

After the JDK1.5 version, the above two lines of code can be compiled through, it is because JDK1.5 after adding new features, automatic boxing.

The first code is the normal way to create an object, creating an Integer wrapper class object.

In the second sentence, when the left Interger type variable points to the right int primitive type data, the base data type on the right is automatically boxed into an integer object, which implicitly executes the new integer (4).

Here's another piece of code:

1         New Integer (4); 2         x = x + 2;

These two sentences can also be compiled, and this is also because the JDK1.5 has been added to the automatic unpacking feature.

In the second sentence, when an integer object on the right is adding to the INT basic data type data, the integer object is automatically disassembled, the X.intvalue () is implicitly executed, the integer object is converted to the INT basic data type data, and then X.intvalue () +2 draw and, finally, and automatically boxed into an integer object, new Integer (X.intvalue () +2). This way, the left integer variable x succeeds in pointing to a new integer object.

As can be seen from the above example, the new features of the automatic unpacking box can simplify the code and make it more convenient for us to write the program.

But there are also areas to note in the use, see the following code:

1Integer A =NewInteger (100);2Integer B =NewInteger (100);3System.out.println ("A==b:" + (a==b));//the result is false. 4 5Integer C = 100;6Integer d = 100;7System.out.println ("C==d:" + (C==d));//The result is true. 8          9Integer m = 200;TenInteger n = 200; OneSystem.out.println ("M==n:" + (m==n));//the result is false. 

As you can see from this code, the size of the int base data type in the auto-unpacking process affects the same object that the integer variable ultimately points to, which is why?

The three comparisons of this code have the following differences:

1, if the object is created in the normal way (new) to make a reference to the variable, because different variables point to different objects, the comparison between different variables is of course false.

2, if the integer variable is in the form of automatic boxing, the reference to the object, and the size of the int value before boxing is exactly within the range of byte ( -128~127), then, If more than one integer variable points to an integer object that is boxed with the same int value, Java only creates the first integer object in memory, and all the other variables point to the object, so the variables are compared because they all point to the same object. The result is true.

3, if the integer variable uses the automatic boxing form, the reference to the object, and the size of the int value before boxing is not in the range of Byte ( -128~127), then, even if the value of int is the same, Java will create multiple objects in the corresponding variable in memory, and when multiple variables are compared, They point to different objects, so the result is false. From the result, this is the same as creating an object reference with new.

The above examples are small details of some applications, the details of the study, the Java Road will be more flat.

Java Fundamentals: New features after JDK1.5: Automatic disassembly box, and precautions

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.