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: