Home  >  Article  >  Java  >  A brief discussion on what is java's automatic boxing and automatic unboxing?

A brief discussion on what is java's automatic boxing and automatic unboxing?

青灯夜游
青灯夜游forward
2018-10-19 17:43:022694browse

The content of this article is to briefly talk about what is java's automatic boxing and automatic unboxing? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Auto-unboxing and auto-boxing

Java provides corresponding wrapper types for each basic data type. For example:

public class TestMain
{
    public static void main(String[] args)
    {
        Integer i = 10;
    }
}

In this process, the corresponding Integer object will be automatically created based on the value. This is automatic boxing . Look at another piece of code:

public class TestMain
{
    public static void main(String[] args)
    {
        Integer integer = 10;
        int i = integer;
    }
}

In this process, the data will be automatically converted into a basic type according to the wrapper type, which is automatic unboxing.

As for the principle of automatic boxing and automatic unboxing, it is also very simple. Through the command line program, enter CLASSPATH (that is, the path where the .class file in the bin directory is located), decompile with javap and check the generated bytecode:

There is a lot of decompiled content, we only focus on the key points Part:

public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=1
         0: iconst_1
         1: invokestatic  #16                 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
         4: astore_1
         5: aload_1
         6: invokevirtual #22                 // Method java/lang/Integer.intVal
ue:()I
         9: istore_2
        10: return

See that during automatic boxing, the Java virtual machine automatically calls the valueOf method of Integer; during automatic unboxing When , the Java virtual machine automatically calls the intValue method of Integer. This isThe principle of automatic unboxing and automatic packing.

Beware of null pointer exception

There is such a piece of code:

public static void main(String[] args) throws Exception
{
    Object obj = getObj(null);
    int i = (Integer)obj;
}
    
public static Object getObj(Object obj)
{
    return obj;
}

This usage scenario is very common. We put an int value in In session or request, when it is taken out, it will be a scene similar to the above. Therefore, be careful about null pointer exceptions during automatic unboxing.

小trap

Look at the two pieces of code, the first piece of code is:

public class TestMain
{
    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);
    }
}

The running result is:

true
false

The second piece of code is:

public class TestMain
{
    public static void main(String[] args)
    {
        Double d1 = 100.0;
        Double d2 = 100.0;
        Double d3 = 200.0;
        Double d4 = 200.0;
        
        System.out.println(d1 == d2);
        System.out.println(d3 == d4);
    }
}

The running result is:

false
false

The reason for such a result is: valueOf() of the boxing classes of Byte, Short, Integer, Long, and Char The method is cached based on the 128-bit dividing line. If the value is below 128 and above -128, the reference in the cache will be taken. Taking Integer as an example, the source code of valueOf(int i) is:

public static Integer valueOf(int i) {
    final int offset = 128;
    if (i >= -128 && i <= 127) { // must cache 
        return IntegerCache.cache[i + offset];
    }
        return new Integer(i);
    }

Float and Double will not. The reason is also very simple, because the number of integers in a certain range of byte, Short, integer, long, and char is limited, but the two floating point numbers of float and double are not. no.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit Java video tutorial, java development graphic tutorial, bootstrap video tutorial!

The above is the detailed content of A brief discussion on what is java's automatic boxing and automatic unboxing?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete