Understanding of java Learning Characteristics of Automatic Unpacking Box

  • 2021-11-13 07:49:30
  • OfStack

Directory 1. What is automatic packing and unpacking? 2. Use of unpacking boxes 1. Reasons for introducing packaging classes 2. Use of automatic unpacking boxes 3.1 Some typical examples

1. What is automatic packing and unpacking?

Sometimes, you need to convert a basic type like int into an object. All primitive types have one corresponding class, for example, the Integer class corresponds to the primitive type int. Typically, these classes are called wrappers. These wrapper classes correspond to basic data types:
Integer , Long , Float , Short , Byte, Charater , Boolen;
(The first six classes are derived from the common superclass Number) The wrapper class is immutable, that is, once a wrapper is constructed, it is not allowed to change the value wrapped in it. At the same time, the wrapper class is final, so it cannot derive its subclasses.

2. Use of disassembly boxes

1. Reasons for introducing wrapper classes:

Wrapped into an object is more convenient to operate, for example, you can directly encapsulate a few methods in a class for easy use: for example, converting a numeric string s into a numeric value can be used: int x = Integer. parseInt (s), or you can easily add data to a collection: if you want to define an integer array list. Unfortunately, the type parameters in angle brackets are not allowed to be primitive types, that is, they are not allowed to be written as ArrayList, so the Integer wrapper class can be used here.
We can declare an array list of Integer objects:
ArrayList list = new ArrayList()

Note: ArrayList performs much less efficiently than int [] arrays because each value is wrapped separately in an object. Therefore, this construct is considered for smaller collections only when the convenience of operation is more important than the efficiency of execution

2. Application of automatic disassembly box

1. The auto-boxing feature is used for adding elements of type int to ArrayList (), with the following call:
list. add (3);
Will be automatically converted to:
list.add(Integer.valueOf(3));
This is automatic packing (autoboxing)

2. Conversely, when an Integer object is assigned to an int value, the box is automatically unpacked. That is, the compiler sets the following statement:
int n=list.get(i);
Convert to:
int n =list.get(i).intValue();
This is automatic unpacking

3.1 Some typical examples

1. Automatic unpacking is applicable to arithmetic expressions. For example, you can apply the self-increasing operator to a wrapper class reference:
Integer n =1;
n++;
The compiler will automatically insert an object unpacking instruction, then carry out self-increment operation, and finally unpack the result.

2. Note for associating the = = operator with a wrapper class
(The following is about running constant measuring pools. If you need to find out, please go to this article.)

Understanding of measuring pool when portal is running

(1) valueOf is the common method for automatic packing, and intValue is the method for automatic unpacking. In their source code, there is 1 piece of code that defines the buffer of wrapper class. Except for two wrapper classes, Long and Double, which do not implement this caching technique, all the other wrapper classes do.
The following code:


Integer i1 = 40;
Integer i2 = 40;
Double i3 = 40.0 ; 
Double i4 = 40.0 ; 
System.out.println("i1=i2   " + (i1 == i2));
System.out.println("i3=i4   " + (i3 == i4));

--Results--
true
false

The principle is as follows:
"= =" This operation will not be unpacked automatically without arithmetic operators, so i1 and i 2 are not numerical comparison, but still compare whether the address points to the same block of memory. The buffer range in integer source code is-128-127. In this range, as long as new objects are not included, the numerical addresses are all the same and are in the constant pool area. However, Double has no buffer, so the addresses of i3 i4 are different.
Therefore, it is best to call equals method when wrapping class object comparison.

3. (1) Since the wrapper class reference can be NULL, auto-boxing may throw an NullPointerException exception:
eg: Integer n =null; System.out.println(2*n);
(2) In addition, if Integer and Double are mixed in a conditional expression, the value of Integer will be unpacked, promoted to double and then packed to Double;

Reference-"java Core Technology Volume 1"

The above is the java learning to understand the features of automatic unpacking in detail, more information about java automatic unpacking please pay attention to other related articles on this site!


Related articles: