[Turn]java automatic packing and unpacking

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/shenliang123/archive/2012/04/16/2451996.html

This is jdk1.5 after the introduction of new content, as adhering to the publication is the best memory, decided to use a blog to replace my memory:

The Java language Specification says that in many cases packaging and unpacking are done by the compiler itself (in which case the wrapper becomes boxed, and the wrapper is called unboxing);

In fact, according to my own understanding, automatic boxing can be easily understood as the basic data type encapsulated as an object type, to conform to the Java object-oriented, for example, an int to illustrate:

Declaring an integer object integer num = 10;//is used for automatic boxing: resolves to an integer num = new integer (10);

The above is a good embodiment, because 10 is a basic data type, in principle it is not directly assigned to an object integer, but after jdk1.5 you can make such a declaration, which is the charm of automatic boxing
Automatically converts the base data type to the corresponding package type. All of the methods declared by the object can be called after being an object

Auto-unpacking: The Name Incredibles is to re-convert the object to the basic data type:

Boxed integer num = 10;//unboxing int num1 = num;

A typical use of automatic unpacking is when the operation is performed: Because the object is not directly operational, it is converted to the basic data type before it can be subtraction

The Integer num = 10;//is suppressed by the automatic unpacking System.out.print (num--) when calculating;

Haha should feel very simple, the following I would like to point out a little bit more difficult, is a little
Look at the following example, if the difference between = = and equal is not clear when looking at the following example, you can first see http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452156.html

Number of integers outside the -128~127         num1 = 297;   Integer num2 = 297;                    System.out.println ("num1==num2:" + (NUM1==NUM2));                             Number of integers within -128~127          = num3;   Integer num4 = 97;            

The result of printing is: num1==num2: false num3==num4: true

It's strange: This is due to Java's design of the automatic boxing and unpacking of integers and int, which is a pattern: call-to-enjoy meta-mode (flyweight)

In order to increase the reuse of simple numbers, Java defines: when auto-boxing for values from –128 to 127, they are boxed as an integer object, there is always only one object in memory that is reused.

If the value from –128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating an integer object each time the box is boxed; you see?

The above phenomenon is caused by the use of automatic boxing, if you do not use automatic boxing, but the same as the General class, with new to instantiate, will be a new object whenever new;

This automatic boxing unboxing is not only used in the basic data types, but also in the string class, such as when we declare a string object often:

String str = "SL";//instead of the following declaration method, String str = new String ("SL");

[Turn]java automatic packing and unpacking

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.