Detailed explanation of Java packaging class and automatic packing and unpacking

  • 2021-07-09 08:24:11
  • OfStack

Java wrapper class

基本类型 大小 包装器类型
boolean / Boolean
char 16bit Boolean
byte 8bit Byte
short /16bit Short
int 32bit Integer
long 64bit Long
float 32bit Float
double 64bit Double
void / Void

The wrapper class of Java serves two main purposes:

The Java wrapper class "wraps" values of primitive data types into objects, and operations on primitive data types become operations on objects, enabling primitive values to be included in operations reserved for objects. For example, add elements to Collections (generic operations restrict the addition to objects only, such as List = new ArrayList () is incorrectly written), or return from methods with object return values. It is more convenient to convert types, such as the common conversion from Integer to characters

Packing and unpacking

Java provides automatic packing and unpacking mechanisms after SE5. Basic data types can be automatically converted to and from their corresponding wrapper classes

Such as:


Integer i = 10;
int index = i;

Boxing automatically converts a base data type to a wrapper type
Unpacking is automatically changing the wrapper type to the basic data type

The valueOf (int) method of Integer is automatically called at the time of boxing. The intValue method of Integer is automatically called when unpacking.

Others are similar, such as Double and Character. Friends who don't believe it can try it manually.

Therefore, the realization process of packing and unpacking can be summarized in one sentence:

The packing process is implemented by calling the wrapper's valueOf method, while the unpacking process is implemented by calling the wrapper's xxxValue method. (xxx represents the corresponding base data type).

Interview questions

What is the output of the following code?


public class Main {
 public static void main(String[] args) {
 Integer i1 = 100;
 Integer i2 = 100;
 Integer i3 = 200;
 Integer i4 = 200;
 
 System.out.println(i1==i2);
 System.out.println(i3==i4);
 }
}

Note the difference between = = and equals:
Type = = equals
--
Base data type value is not available
Wrapper class address content

The output result is:

true
false

Why did this happen? The output shows that i1 and i2 point to the same object, while i3 and i4 point to different objects. At this point only need 1 look at the source code to know, the following code is Integer valueOf method of the specific implementation:


public static Integer valueOf(int i) {
 if(i >= -128 && i <= IntegerCache.high)
  return IntegerCache.cache[i + 128];
 else
  return new Integer(i);
 }

It can be seen from these two pieces of code that when creating Integer object by valueOf method, if the value is between [-128, 127], it will return a reference to the existing object in IntegerCache. cache; Otherwise, a new Integer object is created.

In the above code, the values of i1 and i2 are 100, so the existing objects will be taken directly from cache, so i1 and i2 point to the same object, while i3 and i4 point to different objects respectively.

What is the output of the following code?


public class Main {
 public static void main(String[] args) {
 Double i1 = 100.0;
 Double i2 = 100.0;
 Double i3 = 200.0;
 Double i4 = 200.0;
 
 System.out.println(i1==i2);
 System.out.println(i3==i4);
 }
}

Maybe some friends will think that the output result is the same as that of the above question, but in fact it is not. The actual output is:

false
false

As to why, the reader can look at the valueOf implementation of the Double class.
Here we will only explain why the valueOf method of the Double class under 1 has a different implementation than the valueOf method of the Integer class. Simple: There is a finite number of integer values in a range, but floating-point numbers are not.

Note that the implementation of the valueOf method for the classes Integer, Short, Byte, Character, and Long is similar, and the implementation of the valueOf method for the classes Double, Float is similar


Related articles: