Java automatic packing and unpacking traps

Source: Internet
Author: User
The function of automatic packing and unpacking is in fact the compiler to help you. During the compilation period, the compiler determines whether to perform packing or unpacking Based on the syntax you have compiled. For example:

Integer I = 100; equivalent to the compiler automatically compile the following syntax for you:

Integer I = new INTEGER (100); therefore, the function of automatic packing and unpacking is called "compiler Sugar". Although this function is very convenient, however, you must understand the semantics of Java when the program is running. For example, the following program can be compiled:

Integer I = NULL; Int J = I; this syntax is valid during the compilation period, but there will be errors during the runtime, because this syntax is equivalent:

Integer I = NULL; Int J = I. intvalue (); null indicates that I has not referenced any object entity, and it can be legally specified to the object reference name. In fact, I does not reference any objects, so it is impossible to operate the intvalue () method. In this way, the nullpointerexception error occurs during the above writing.

The function of automatic packing and unpacking provides convenience, but hides some details, so be careful. Let's look at example 4.6. What do you think is the result?

Ü example 4.6 autoboxdemo2.java

Public class autoboxdemo2 {

Public static void main (string [] ARGs ){

Integer I1 = 100;

Integer I2 = 100;

If (I1 = I2)

System. Out. println ("I1 = I2 ");

Else

System. Out. println ("I1! = I2 ");

}

}

From the mechanism of automatic packing and unpacking, you may feel that the result is I1 = I2, you are right. So what do you think is the result of this program in example 4.7?

Ü example 4.7 autoboxdemo3.java

Public class autoboxdemo3 {

Public static void main (string [] ARGs ){

Integer I1 = 200;

Integer I2 = 200;

If (I1 = I2)

System. Out. println ("I1 = I2 ");

Else

System. Out. println ("I1! = I2 ");

}

}

The result is I1! = I2, which is somewhat surprising. the syntax of the two examples is the same, but the result is the opposite.

In fact, this is related to the = Operator. In chapter 3rd, = is used to compare the values of two basic data types, in fact, = is also used to determine whether two object reference names refer to the same object.

When auto-packing is performed for values ranging from-128 to 127, they are packed as integer objects and will be reused in memory, therefore, when using = for comparison in example 4.6, I1 and I2 actually refer to the same object. If the value range from-128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating an integer object each time it is packed, therefore, for example 4.7, when using = for comparison, I1 and I2 refer to different objects.

Therefore, do not rely too much on automatic packing and unpacking. You must know the differences between basic data types and objects. Example 4.7 is best written in a regular way, rather than relying on the compiler sugar (compiler sugar ). For example, it is correct that sample 4.7 must be rewritten as sample 4.8.

Ü example 4.8 autoboxdemo4.java

Public class autoboxdemo4 {

Public static void main (string [] ARGs ){

Integer I1 = 200;

Integer I2 = 200;

If (i1.equals (I2 ))

System. Out. println ("I1 = I2 ");

Else

System. Out. println ("I1! = I2 ");

}

}

The result is I1 = I2. I believe this writing method will be more reassuring. Do you want to use these convenient but detail-hiding functions? There is basically only one principle: Do not use it if you are not sure.
 

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.