Java Boxing and unpacking detailed

Source: Internet
Author: User
Tags implement integer numeric value wrapper

The problem of automatic boxing and unboxing is a cliché in Java, and today we'll take a look at some of the problems in boxing and unboxing. This article first about boxing and unpacking the most basic things, and then look at the interview written in the often encountered with the box, unpacking related issues.

The following is a table of contents outline for this article:

A. What is boxing? What is a unpacking case?

Two. How to implement the boxing and unboxing

Three. Related questions in the interview

If there are any mistakes, please understand and criticize them, I appreciate it.

Please respect the results of the work of the author, reproduced please indicate the original link:

Http://www.cnblogs.com/dolphin0520/p/3780005.html

A. What is boxing? What is a unpacking case?

As mentioned in the previous article, Java provides the corresponding wrapper type for each of the basic data types, and the reason why the wrapper type is provided for each of the basic data types is not elaborated here, and interested friends can access the relevant information. Before Java SE5, you must do this if you want to generate an integer object that has a value of 10:

Integer i = Newinteger (10);

The automatic boxing feature is provided from the Java SE5, and if you want to generate an integer with a value of 10, this is all you need to do:

Integer i = 10;

This process automatically creates the corresponding integer object based on the numeric value, which is the boxing.

So what is a unpacking? As the name suggests, the corresponding to the boxing, is to automatically convert the wrapper type to the basic data type:

Integer i = ten;  Boxed
int n = i;   Split Box

Simply put, boxing is the automatic conversion of the base data type to the wrapper type, and the unboxing automatically converts the wrapper type to the base data type.

The following table is the wrapper type for the base data type:

Two. How to implement the boxing and unboxing

After the basic concept of boxing is understood in the previous section, this section is about how boxing and unboxing are implemented.

Let's take the Interger class for example, and look at the following code:

public class Main {public
    static void Main (string[] args) {
             
        Integer i = ten;
        int n = i;
    }
}

After you decompile the class file, you get the following:

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.