The basic types and wrapper classes often need to be converted to each other, in the case of an Integer (the operation of several other packaging classes is similar):
With the introduction of the automatic boxing and unpacking mechanism in JDK1.5 , the conversion between the wrapper class and the base type is easier and more convenient.
So what is packing and unpacking? Let's look at each other.
Boxing: converts the basic type into a wrapper class , which has the properties of the object and can be divided into manual boxing and automatic packing .
unpacking: In contrast to boxing, the wrapper class object is converted to a basic type of value, and can be divided into manual unpacking and automatic unpacking
The following code example illustrates the process of boxing and unpacking:
The result is:
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //defining a double type variable5 DoubleA = 91.5;6 7 //Manual Boxing8Double B =NewDouble (a); 9 Ten //Automatic Boxing OneDouble C =A; A -SYSTEM.OUT.PRINTLN ("Boxed result is:" + B + "and" +c); - the //defines a double wrapper class object with a value of 8 -Double d =NewDouble (87.0); - - //Manual Unpacking + DoubleE =D.doublevalue (); - + //Automatic Unpacking A Doublef =D; at -SYSTEM.OUT.PRINTLN ("The result after unpacking is:" + E + "and" +f); - } -}
Java Fundamentals (35): Boxing and unpacking---conversions between basic types and wrapper classes in Java