Beginners learn JAVA (4) ---- packing and unpacking, java ----

Source: Internet
Author: User

Beginners learn JAVA (4) ---- packing and unpacking, java ----
1. What is packing? What is unpacking?

In Java, Java provides the object package type for each basic data type. See the following table:

Packaging class table

Basic Data Type Packaging
Byte Byte
Boolean Boolean
Short Short
Char Character
Int Integer
Long Long
Float Float
Double Double


Before Java SE5, if you want to generate an Integer object with a value of 50, you need to do this:

Integer obj = new Integer(50);

In Java SE5, Java encapsulates the feature of automatic packing, and defines the value as follows:

Integer obj = 50;

In this process, an Integer object is automatically created based on the value, which is called packing;

Simply put, packing means to automatically convert the basic data type to the package type; unpacking means to automatically convert the package type to the basic data type.

So what is unpacking? And the packing object is to convert the package type to the basic data type. The disassembly and packing process is as follows:


How is the disassembly box implemented?

Taking the Double type as an example, let's take a look at the following code:

public class Main{        public static void main(String[] args){                Double DB = 10.34;                double db=DB;        }}

Then we decompile the code. The result is as follows:

Next, let's change it to the Integer type and try again. Let's look at the result directly:

We can see what is the same. The program automatically calls the valueof method when automatically packing, and calls the corresponding type of "**. ** Value" method when unpacking.

From this we can see that the automatic packing of a program calls the valueof method of the package type, while the unpacking is implemented by calling the "**. ** Value" method of the package.

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.