In the previous article, we briefly introduced the basic concepts of packaging and briefly analyzed the source code of several core methods in the Integer class. However, the concept of automatic unboxing was not fully introduced due to the space. In this article, we will also analyze several common packaging interview questions to gain an in-depth understanding of our packaging class design.

Automatic unpacking

The so-called “unpacking” refers to the process of packaging type conversion to basic type, and the so-called “packing” is the process of basic type to packaging type. Such as:

public static void main(String[] args){
    int age = 21;
    Integer integer= new Integer(age); Int num = integer.intValue(); / / devanning}Copy the code

Since the introduction of auto-unboxing in JDK1.5, the above code can be simplified as follows:

public static void main(String[] args){
    int age = 21;
    Integer integer= age; // automatic boxing int num =integer; // automatic unpacking}Copy the code

If it feels a lot easier, but there is no change at the JVM level, this is an “illusion” made by the compiler.

The compiler allows you to write code this way, but when compiled into bytecode instructions, the compiler still calls the corresponding unboxing method.

As you can see, unboxing requires method calls, which require the loading and unloading of stack frames. Frankly speaking, it consumes resources, so we should try to avoid a lot of “unboxing” operations in our program.

The interview questions

Interview question 1:

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);
}
Copy the code

Anyone who hasn’t read Integer’s internal source code before will be “scratching their heads” at the output.

The output is:

true
false
Copy the code

If you read my two articles carefully, this question should not be difficult to explain.

Assigning an Integer value directly to an Integer instance will result in a boxing operation, which calls the valueOf method, and this method we analyzed first checks to see if 100 is cached in the cache pool, Of course IntegerCache will cache Integer instances between [-128,127] by default, so the reference is assigned to variable i1 directly from the cache pool.

Similarly, i2 takes a reference from the cache pool, and both references the same heap object, which is why it prints “true”.

The second output “false” is also understandable because 200 is not in the cache pool cache, so each call to valueOf creates a different instance of Integer.

Interview question 2:

Public static void main(String[] args){Double i1 = 100.0; Double i2 = 100.0; Double i3 = 200.0; Double i2 = 200.0; System.out.println(i1==i2); System.out.println(i3==i4); }Copy the code

Many people would think that this code would produce the same output as above, but it does not:

false
false
Copy the code

That’s because the wrapper class Double has no concept of a cache pool, meaning that it wraps a new instance of a Double for each value. As with its valueOf method:

public static Double valueOf(double d) {
    return new Double(d);
}
Copy the code

One might wonder why Integer uses a cache pool to improve efficiency, but not Double.

Given an Integer [-128.0,127.0], can you determine how many double values there are in the interval?

Because any interval, no matter how small, corresponds to an infinite number of double decimals, it cannot be cached.

The same problem applies to Float.

Interview question 3:

public static void main(String[] args){
    Boolean i1 = false;
    Boolean i2 = false;
    Boolean i3 = true;
    Boolean i4 = true;

    System.out.println(i1==i2);
    System.out.println(i3==i4);
}
Copy the code

Output result:

true
true
Copy the code

The Boolean valueOf method looks like this:

public static final Boolean TRUE = new Boolean(true);

public static final Boolean FALSE = new Boolean(false);

public static Boolean valueOf(boolean b) {
    return (b ? TRUE : FALSE);
}
Copy the code

Clearly, the results need no further explanation.

Finally, five of the eight wrapper classes support “cache pooling.”

  • Integer: Indicates that the type of the cache pool is IntegerCache
  • Byte: Indicates that the cache pool type is ByteCache
  • Short: The corresponding cache pool type is ShortCache
  • Long: The corresponding cache pool type is LongCache
  • Character: The corresponding cache pool type is CharacterCache

In fact, the implementation of Boolean is special, because it has only two possible values, in fact, can be counted as caching.


All the code, images and files in this article are stored in the cloud on my GitHub:

(https://github.com/SingleYam/overview_java)

Welcome to wechat public number: jump on the code of Gorky, all articles will be synchronized in the public number.