title: "java auto unpacking"
tags:
- Java
The process of encapsulating a basic data type into an object is called boxing, whereas the process of converting a wrapper class corresponding to a basic data type into an object is called unboxing;
Differences between basic data types and other objects
Basic data type
Java is an object-oriented strong type language, but it is not like python that everything is object-oriented. Some of the most frequently used data structures in Java are not object-oriented. They are basic data types, also known as built-in types. They are stored in the stack, which is more efficient than other objects created with new. Java has 9 basic data types, which are divided into five categories
type | identifier | Remarks |
---|---|---|
integer | byte, short, int, long | |
floating-point | float, dauble | |
character | char | |
Boer | boolean | |
empty | void | Inoperable |
Range of basic data types
Range of integers
- Byte: takes up one byte, that is, 8 bits. The highest bit is the symbol bit, and the effective bit is only 7 bits (stored by complement).
Maximum: 0111 1111 (127) Minimum: 1000 0000 (- 128)
What's the calculation?
The highest digit is the sign digit, which is fixed. The positive digit is represented by 0, the negative digit is represented by 1, and then the following seven digits are represented by 7 ones at most and 7 zeros at least. This is the maximum and the minimum that the byte represented by the complement can represent. Convert the complement to the original code (the complement of the positive digit is the source code, and the complement of the negative digit is inverted plus one) and then convert it to 10 base.
- short: two bytes, 16 bits, valid 15 bits
Maximum: 2 ^ 15 - 1: 32767 Minimum: - (2 ^ 15): - 32768
- int: takes up 4 bytes, maximum [2 ^ 31 - 1] (2147483647), minimum [- 2 ^ 31] (- 2147483648)
- long: 8 bytes, maximum value [2 ^ 63 - 1] (9223372036854775807), minimum value [- 2 ^ 63] (- 9223372036854775808)
Packaging type
Other objects in Java are inherited from objects and have their own properties and methods. In order to facilitate the operation of basic data types and other objects, Java provides corresponding packing types for each basic data type,
Basic data type | Packaging class |
---|---|
byte | Byte |
boolean | Boolean |
short | Short |
char | Character |
int | Integer |
long | Long |
float | Float |
double | Double |
- Why use packing type
Java is an object-oriented language. Most operations are object-oriented, such as containers. The largest range that can be stored in containers is objects, and the basic data type does not belong to objects, so it cannot be stored in containers. In order to solve this problem, the basic data type must be "wrapped" so that it can participate in programming as an object.
boxing and unboxing
The process of packing basic data types into objects is called boxing, while the process of converting objects into basic data types is called unpacking.
After Java SE5, in order to simplify development, an automatic packing and unpacking mechanism is provided. Java will automatically convert basic data types and packaging types at the right time, such as:
public class Demo1 { public static void main(String[] args) { Integer integer = 3; // Automatic boxing int i = integer; // Automatic dismantling } }
Obtained by decompiling with Javap
public static void main(java.lang.String[]); Code: 0: iconst_3 1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: astore_1 5: aload_1 6: invokevirtual #3 // Method java/lang/Integer.intValue:()I 9: istore_2 10: return
In case of automatic packing, the valueOf() method of the packing class is called, while in case of automatic unpacking, the intValue() method of the packing class is called, so if there is no automatic unpacking mechanism before Java se 5, we need to write the above code as follows
public class Demo1 { public static void main(String[] args) { Integer integer = Integer.valueOf(3); // Boxing int i = Integer.intValue(integer); // Unpacking } }
In addition to int and Integer, the automatic conversion of other basic types and wrapper classes is the same. The valueOf() method is called when boxing, and the xxxValue() method is called when unpacking.
When is automatic packing
1. Initialization, assignment, function return
When the basic data type is assigned to the wrapper class or the basic data type is used as the function return value but the function declaration requires the return of the wrapper type, it will be automatically boxed, as shown in the above example
2. Put the basic data type into the container
public void func2(){ List<Integer> list = new ArrayList<>(); list.add(1); }
After disassembly
public void func2(); Code: 0: new #4 // class java/util/ArrayList 3: dup 4: invokespecial #5 // Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: iconst_1 10: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 13: invokeinterface #6, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 18: pop 19: return
In step 10, automatic boxing is used
When is the automatic unpacking
1. Initialization, assignment, function return
When a wrapper class object is assigned to a variable of basic data type, it will be unpacked automatically (the principle of function return value is the same)
2. When calculating the packing type
Arithmetic operations (including comparison size) are for basic data types. Therefore, operations between basic data type and packing type, packing type and packing type will be converted to two basic data types
public void func3(){ Integer integer = 3; int i = 1; Integer integer1 = 1; boolean b1 = integer > i; // Comparison size of basic data type and packing type boolean b2 = integer > integer1; // Compare sizes of two packaging types }
Anti compilation
public void func3(); Code: 0: iconst_3 1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: astore_1 5: iconst_1 6: istore_2 7: iconst_1 8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 11: astore_3 12: aload_1 13: invokevirtual #3 // Method java/lang/Integer.intValue:()I 16: iload_2 17: if_icmple 24 20: iconst_1 21: goto 25 24: iconst_0 25: istore 4 27: aload_1 28: invokevirtual #3 // Method java/lang/Integer.intValue:()I 31: aload_3 32: invokevirtual #3 // Method java/lang/Integer.intValue:()I 35: if_icmple 42 38: iconst_1 39: goto 43 42: iconst_0 43: istore 5 45: return
Through step 13, it is explained that the comparison size of basic data type and packing type will be converted to two basic data types for re comparison
Through steps 28 and 32, it is indicated that the comparison size of the two packing types will also be converted to the basic data type
It's the same with normal addition, subtraction, multiplication and division
public void func4() { Integer integer = 3; int i = 1; Integer integer1 = 1; int s = integer + integer1; int s1 = integer + i; }
public void func4(); Code: //... 13: invokevirtual #3 // Method java/lang/Integer.intValue:()I 16: aload_3 17: invokevirtual #3 // Method java/lang/Integer.intValue:()I 20: iadd 21: istore 4 23: aload_1 24: invokevirtual #3 // Method java/lang/Integer.intValue:()I //...
3. Binocular operation
If the second and third digit of the three item operation is the basic data type and the other is the packing type, the two basic data types will be unpacked automatically
public void func5() { boolean flag = true; Integer i = 8; int j; j = 3; int k = flag ? i: j; }
public void func5(); Code: 0: iconst_1 1: istore_1 2: bipush 8 4: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 7: astore_2 8: iconst_3 9: istore_3 10: iload_1 11: ifeq 21 14: aload_2 15: invokevirtual #3 // Method java/lang/Integer.intValue:()I 18: goto 22 21: iload_3 22: istore 4 24: return
Because i is the packing type and j is the basic data type, i is automatically unpacked into the basic data type in line 14 (it is not the int that should be returned for the trinary operation). Therefore, attention should be paid when doing the trinary operation, especially when the basic data type and the object are mixed, if the object is not assigned, NPL (null pointer exception) may be caused
Only one is the basic data type and the other is the packing class object. Two objects are not opened.
Cache problems in packing and unpacking
public class Demo2 { public static void main(String[] args) { Integer a1 = 1; Integer a2 = 1; int a3 = 1; System.out.println(a1 == a2); // true System.out.println(a1.equals(a2)); // true System.out.println(a1 == a3); // true System.out.println(a1.equals(a3)); // true } }
public class Demo2 { public static void main(String[] args) { Integer a1 = 133; Integer a2 = 133; int a3 = 133; boolean b1 = a1 == a2; boolean b2 = a1.equals(a2); boolean b3 = a1 == a3; boolean b4 = a1.equals(a3); System.out.println(b1); // false System.out.println(b2); // true System.out.println(b3); // true System.out.println(b4); // true } }
The two results are different. The reason is that there is a cache problem when we use Integer for the first time. When we use Integer, Java will initialize an Integer[] cache and add the number between - 128 and 127 to the cache through a loop. If the value of the new Integer is within this range, we will directly return the created object. Is the equal () comparison value the same as = = comparison An object, so in any case, the result of equals is true, while = = is true between - 128 and 127, and false beyond this range.
This is similar to the small integer pool in python, but the scope of python is [- 5, 256]
In addition to integers between [- 128, 127], two values of boolean and characters between \ u0000 and \ u007f are also in the constant pool.
summary
- What is packaging
To facilitate the operation of basic data types, a wrapper class is provided for each basic type. They wrap the basic data type into an object
- What is packing? Unpacking
The process of wrapping a basic data type into a wrapper class is called Boxing (using the valueOf() method of the wrapper class)
The process of converting a wrapper class to a basic data type is called unboxing (using the xxxValue() method of the wrapper class)
- What is automatic packing / unpacking
Java SE5 introduces a mechanism to automatically convert basic data types to wrapper classes / wrapper classes to basic data types in specific situations
-
When is automatic packing
-
Integer a = 5
-
Function return value
-
When adding a basic data type to a container
-
When is the automatic unpacking
-
Initialization, assignment, function return value
-
When the third parameter of the third operation has both packing class and basic data type
-
Arithmetic operations, comparing sizes
-
What is the cache in case of automatic disassembly
When using some wrapper classes for the first time, Java will create a cache pool. Later, every time you need to wrap class objects, you will first find whether there are any in the cache pool, and then directly return them if there are any, and then create them if they are not. Scope of the cache pool:
Integer: [- 128, 127]
Boolean: [true, false]
Characters: [\ u0000 \ u007f]
Reference resources
What is the automatic unpacking box in Java
The caching mechanism of integer in Java