Detailed explanation of automatic unpacking and automatic packing in Java programming

What is automatic packing and unpacking

Automatic packing of basic data types( auto@R_885_2419 @Unpacking( un@R_885_2419 @Ing) is a function provided since J2SE 5.0.

Generally, when we want to create an object instance of a class, we will do the following:

Class a = new Class(parameter);

When we create an integer object, we can do this:

Integer i = 100; (Note: not int i = 100;)

In fact, when executing the above code, the system executes for us: integer I = integer valueOf(100); (thanks for @ black bread and @ maydayit's reminder)

This is the automatic packing function of the basic data type.

To put it simply, boxing is to automatically convert the basic data type to the wrapper type; Unpacking is to automatically convert the wrapper type to the basic data type.

The following table shows the wrapper types corresponding to the basic data types:

Differences between basic data types and objects

The basic data type is not an object, that is, variables and constants defined by int, double, Boolean, etc.

The base data type has no callable methods.

eg: int t = 1; t. There is no way to drop.

Integer t =1; t. There are many methods you can call later.

When is automatic packing

For example: integer I = 100;

This is equivalent to the compiler automatically compiling the following syntax for you: integer I = integer valueOf(100);

When is the automatic unpacking

Automatic unpacking( un@R_885_2419 @Ing), that is, the basic data in the object is automatically fetched from the object. Automatic unpacking can be realized as follows:

Unpacking can also be performed during operation.

Automatic boxing of integer

The output is:

explain:

Equals () compares whether the values (contents) of two objects are the same.

"= =" is used to compare whether the references (memory addresses) of two objects are the same. It is also used to compare whether the variable values of two basic data types are equal.

As mentioned earlier, the automatic boxing of int is that the system executes integer Valueof (int i), first look at integer Java source code:

For values between C128 and 127 (127 by default), integer. Valueof (int i) returns a cached integer object (not a new object)

So in the example, I3 and I4 actually point to the same object.

For other values, execute integer Valueof (int i) returns a new integer object, so in the example, I1 and I2 point to different objects.

Of course, when the auto packing function is not used, the situation is the same as that of ordinary class objects. Please see the following example:

summary

The above is all about the detailed explanation of automatic unpacking and automatic packing in Java programming. I hope it will be helpful to you. Welcome to: detailed explanation of Java array foundation, code example of Java timer usage method, abstract factory code example of Java design pattern notes, etc. if you have any questions, please leave a message and point out. Thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>