Java JDK careful use of boxing

Source: Internet
Author: User
Tags java se

Automatic boxing and unboxing functions in fact, the compiler to help you, the compiler at compile time according to the syntax you write, decide whether to do boxing or unboxing action. For example: Integer i = 100;

The equivalent compiler automatically compiles the following syntax for you: integer i = new integer (100);

So the function of automatic boxing and unpacking is called "Compiler Honey" (Compiler Sugar), although it is convenient to use this function, but you have to understand the semantics of Java during the running stage of the program. For example, the following program can be compiled by:

Integer i = null;

int j = i;

Such syntax is legitimate at compile time, but there are errors at run time because this is equivalent to:

Integer i = null;

Int J = I.intvalue ();

Null means I do not refer to any object entity, which can be legitimately assigned to an object reference name. Since I do not actually refer to any object, it is not possible to manipulate the Intvalue () method so that the above wording will have nullpointerexception errors at run time.

The automatic boxing and unpacking function provides convenience, but hides some details, so be careful. Look at example 4.6 again, what do you think the result is?

Example 4.6 Autoboxdemo2.java

public class AutoBoxDemo2 {
    public static void main(String[] args) {
      Integer i1 = 100;
      Integer i2 = 100;
      if (i1 == i2)
        .out.println("i1 == i2");
      else
        .out.println("i1 != i2");
    }
}

From the automatic boxing and unboxing mechanism, you may feel that the result is displayed 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)
        .out.println("i1 == i2");
      else
        .out.println("i1 != i2");
    }
}

The result is to show I1!= I2, which is a bit surprising, with two paradigm grammars exactly the same, just changing values, but the opposite.

In fact, this is relative to the = = operator, in the 3rd chapter is introduced = = is used to compare the two basic data types of variable values are equal, in fact = = is also used to determine whether two object reference names refer to the same object.

When automatic boxing is used to value values from –128 to 127, they are boxed as integer objects and are reused in memory, so i1 and I2 are actually referenced to the same object when comparing with = = In Example 4.6. If you exceed the value from –128 to 127, the boxed integer object will not be reused, that is, the equivalent of creating an integer object each time it is boxed, so the example 4.7 uses = = to compare, I1 and I2 refer to different objects.

So don't rely too much on automatic boxing and unboxing, you still have to know the difference between basic data types and objects. Example 4.7 is best written in a formal way, rather than relying on compiler honey (Compiler Sugar). For example, the example 4.7 must be rewritten as example 4.8 to be correct.

Example 4.8 Autoboxdemo4.java

public class AutoBoxDemo4 {
    public static void main(String[] args) {
      Integer i1 = 200;
      Integer i2 = 200;
      if (i1.equals(i2))
        .out.println("i1 == i2");
      else
        .out.println("i1 != i2");
    }
}

Results this time is to show i1 = = I2. Use this kind of writing, I believe will also be more comfortable, for these convenient but hide the details of the function in the end want to use it? There's basically one rule: don't use it if you're not sure.

It is recommended that beginners do not use the automatic boxing, unboxing of the syntax, here to explain this function is to integrity to introduce the features of the Java SE 6, beginners, it is better to have a better understanding of the object, then use this function.

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.