The basic data (primitive) type of automatic boxing (autoboxing), unboxing (unboxing) is the function that is provided from J2SE 5.0. While it is convenient for you to pack the basic data types, it is convenient to show that the details are hidden, and it is recommended that you use them to differentiate between the basic data types and the objects.
Autoboxing and unboxing
In Java, everything you want to work with is almost all objects (object), for example, the scanner used previously is an object, a string is also an object, and then more objects are seen. However, the basic (primitive) data type is not an object, that is, a variable that you define with int, double, Boolean, and literal constants that you write directly in your program.
The convenience of manipulating objects has been largely seen in the previous section, and people who have been using Java for some time know that it is sometimes necessary to convert basic data types to objects. For example, when using a map object to manipulate the put () method, the parameters that need to be passed in are objects rather than basic data types.
To wrap a basic data type as an object by using the package type (wrapper Types), you already know in the previous section that before J2SE 5.0, you would use the following statement to wrap an int as an integer object: integer integer = new Integer (10);
The automatic boxing feature is provided after J2SE 5.0, and you can package the base data type directly using the following statement: integer integer = 10;
When compiling, the compiler automatically determines whether an automatic boxing action is made according to the statement you write. The integer reference in the example above is an instance of the integer class. The same action can be applied to basic data types such as Boolean, Byte, short, char, long, float, double, respectively, using the corresponding package type (wrapper Types) Boolean, Byte, short, Character, Long, float, or double. Use the automatic Boxing feature directly below to overwrite example 4.4.
Example 4.5 Autoboxdemo.java
public class AutoBoxDemo {
public static void main(String[] args) {
Integer data1 = 10;
Integer data2 = 20;
// 转为double值再除以3
system.out.println(data1.doubleValue() / 3);
// 进行两个值的比较
system.out.println(data1.compareTo(data2));
}
}
The program seems to be a lot simpler, data1 and data2 in the runtime is an integer instance, you can directly to the object operation. The results of the implementation are as follows:
3.3333333333333335
–1
The method of automatic boxing application can also be as follows:
int i = 10;
Integer integer = i;
You can also use a more generalized Java.lang.Number class for automatic boxing. For example:
Number number = 3.14f;
The 3.14f is automatically boxed as float and assigned to number.
Starting from J2SE 5.0, automatic boxing can also be automatically disassembled (unboxing), which means that the basic data form information in the object is automatically removed from the object. For example, here's what you can say:
Integer Foointeger = 10;
int fooprimitive = Foointeger;
Foointeger reference to an instance of automatic boxing to Integer, if assigned to a variable fooprimitive of type int, it automatically becomes an int type and then assigned to Fooprimitive. In the operation, can also be automatic boxing and unpacking. For example:
Integer i = 10;
System.out.println(i + 10);
System.out.println(i++);
The example above shows 20 and 10, the compiler will automatically boxing and unboxing, that is, 10 will be boxed first, and then at i + 10 o'clock will be first to remove the box, the addition operation; i++ the row is also the first to remove the box and then increment operations. Let's look at one more example:
Boolean Boo = true;
System.out.println (boo && false);
The same boo used to be a Boolean instance, and when the and operation was performed, the Boo was first disassembled, then the and with false, and the result would appear false.