Automatic boxing and unpacking in Java

Source: Internet
Author: User

Before we talk about packing and unpacking, we need to look at the source of this problem first:

The types in Java are divided into basic types (Primitive type) and class types (classes type) in two ways:

Basic types include Byte, char, short, int, long, float, double, and Boolean eight. The base type refers to the direct inclusion of a worthwhile type, which provides language-level support.

Class types include Byte, Character, Short, Integer, Long, Float, Double, Boolean. Why do you use class types? The reason is very simple, because Java is an object-oriented programming language, so manipulating objects is a very general operation, and to produce objects you must first define the class (the class is an abstract object, the object is an instance of the class), and the class type provides more operations than the basic type.

Before adding auto-boxing and unboxing operations to the Java version, if you want to make the base type work like an object, you need to package the base type, such as Long, Integer, Double, Float, Boolean, and so on, which is called a wrapper (Wrapper).

such as: int num=10;

Integer wrapper=new integer (num); Package basic type;

And in the Java1.5 version added automatic boxing and automatic unpacking function, that is, now like an integer num=10; now the real thing is the integer num=new integer (10);

such as: Integer wrapper=10; Automatic packing;

int num=wrapper; Automatic unpacking;

Knowing what is automatic boxing and unpacking, let's take a look at how the program is automatically boxed and removed from the box.

In fact, for an integer num=10, the program expands the code to: integer num=integer.valueof (100);

See what the JDK valueof () is doing:

public static Integer valueOf (int i) {

Assert integercache.high>=127;

if (I>=integercache.low&&i<=integercache.high)

Return integercache.cache[i+ (-integercache.low)];

return new Integer (i);

}

Simply put, if the incoming int is between Integercache.low and Integercache.high, see if the previous cache has been packaged with the same value, return directly, and create a new integer instance without the words.

The reason for this is to elicit the use of "= =" and "Equals ()" problems, such as the following example

Integer i1=200;

Integer i2=200; 200 over Integercache.high default value (127)

if (I1==I2) {

System.out.println ("I1==i2");

}

else{

System.out.println ("I1!=i2");

}

After compiling you will find that the results are not equal, so do not use = = or! = To compare the actual content values of two objects for equality.

Through the problem of automatic boxing and unpacking, the author realizes that on the one hand Java is important to the object and its operability, on the other hand, in the process of learning Java, we should learn to read some of the original Java files, understand the Java implementation principle, in order to know what Java can be used to achieve and what it cannot do.

Automatic boxing and unpacking in Java

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.