Java Syntax sugar 2: auto-boxing and automatic unpacking

Source: Internet
Author: User

At first, I want to learn how automatic unpacking and automatic boxing are attracted to this name, it sounds like a very high-end appearance, in fact, automatic unpacking, automatic boxing is very simple content.

Automatic unpacking and Automatic packing

Java provides a wrapper type for each of the basic data types. As an example:

 Public class testmain{    publicstaticvoid  main (string[] args)    {        =     }}

This process automatically creates the corresponding integer object based on the value, which is automatic boxing. Look at another piece of code:

 Public class testmain{    publicstaticvoid  main (string[] args)    {        = 10 ;         int i = integer;    }}

This process automatically converts the data to the basic type according to the wrapper type, which is the automatic unpacking.

The principle of automatic boxing and automatic unpacking is also simple. From the command-line program, enter Classpath (the path where the. class file is located in the bin directory), and JAVAP to see the resulting bytecode:

There's a lot of anti-compilation stuff, and we're just focusing on the key parts:

1    Public Static voidMain (java.lang.string[]);2 Flags:acc_public, Acc_static3 Code:4Stack=1, locals=3, args_size=150: Iconst_161:invokestatic #16//Method Java/lang/integer.valueo7F: (I) ljava/lang/Integer;84: Astore_195: Aload_1Ten6:invokevirtual #22//Method Java/lang/integer.intval One UE: () I A9: istore_2 -10:return

The Java Virtual opportunity automatically calls the integer valueof method when it is automatically boxed, that is, line 6th, and the Java Virtual Opportunity automatically calls the integer Intvalue method when the box is automatically removed, which is line 10th. This is the principle of automatic unpacking and automatic boxing.

Small traps

Look at the two pieces of code, the first piece of code is:

 Public class testmain{    publicstaticvoid  main (string[] args)    {        = 100 ;         = +;         = $;         = $;                 = = i2)        ; = = i4);}    }

The result of the operation is:

true false

The second piece of code is:

 Public class testmain{    publicstaticvoid  main (string[] args)    {        = 100.0;         = 100.0;         = 200.0;         = 200.0;                 = = D2)        ; = = d4);}    }

The result of the operation is:

false false

The reasons for this result are: Byte, short, Integer, Long, char the valueof () method of the boxed class is cached with a 128-bit dividing line, if it is below 128 and-128 is the value of the cache will be taken from the reference, In the case of integer, the source code for the valueof (int i) is:

 Public Static Integer valueOf (int  i) {    finalint offset =N;     if //         return integercache.cache[i + offset];    }         return New Integer (i);    }

Float, double is not, the reason is also very simple, because Byte, short, Integer, long, char in a range of integer number is limited, but float, double these two floating-point numbers are not. Regarding this little knowledge point, the individual made two comments:

1, not important, in addition to interview to explore the level of knowledge of job seekers, not much use

2, the mind to have the concept of caching , caching for the program to improve the efficiency, save space is a great help

Java Syntax sugar 2: auto-boxing and automatic unpacking

Related Article

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.