Java automatic packing and unpacking

Source: Internet
Author: User

 

This is a new content introduced after jdk1.5. As the best memory for posting, I decided to replace my memory with a blog:

The java language specification says: in many cases, packaging and unpackaging are done by the compiler itself (in this case, packaging is packed, and unpackaging is called unpacking );

In fact, according to my own understanding, automatic packing can be simply understood as encapsulating basic data types as object types to conform to java's object-oriented model. For example, int is used as an example:

// Declare an Integer object
Integer num = 10;
// The above statement uses automatic packing: resolved
Integer num = new Integer (10); The above is a good embodiment, because 10 belongs to the basic data type, in principle it cannot be directly assigned to an object Integer, but after jdk1.5, you can make such a statement, which is the charm of automatic packing.
The basic data type is automatically converted to the corresponding encapsulation type. After becoming an object, you can call all the methods declared by the object.

Automatic box unboxing: the object is renamed as the basic data type:

// Packing
Integer num = 10;
// Unpack
Int num1 = num; a typical use of automatic box unboxing is to perform an operation: because the object is not processed directly, it must be converted to the basic data type before addition, subtraction, multiplication, and division can be performed.

Integer num = 10;
// Automatically Unbox hidden during Calculation
System. out. print (num --); Haha, it should be very simple. Next let's talk about some difficult points,
// In-128 ~ Number not greater than 127
Integer num1 = 297; Integer num2 = 297;
System. out. println ("num1 = num2:" + (num1 = num2 ));
// In-128 ~ Number within 127
Integer num3 = 97; Integer num4 = 97;
System. out. println ("num3 = num4:" + (num3 = num4); the printed result is: num1 = num2: false num3 = num4: true

Strange: this is due to java's automatic packing and unpacking Design for Integer and int. It is a mode called flyweight)

To increase reuse of simple numbers, java defines that after values from-128 to 127 are packed into Integer objects during automatic packing, memory is reused, and only one object exists.

If the value range from-128 to 127 is exceeded, the boxed Integer object will not be reused, which is equivalent to creating an Integer object each time it is packed.

The above phenomenon is caused by the use of automatic packing. If you do not use automatic packing, but like a general class, use new for instantiation, A new object is created every time new is created;

This automatic packing and unpacking method is not only applicable to the basic data type, but also in the String class. For example, when we declare a String object frequently:

String str = "sl ";
// Replace the following declaration method
String str = new String ("sl ");

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.