Home  >  Article  >  Java  >  How to correctly do boxing and unboxing of wrapper classes in Java?

How to correctly do boxing and unboxing of wrapper classes in Java?

PHPz
PHPzforward
2023-05-10 08:43:271505browse

    1. Boxing

    1. Concept

    The so-called boxing (Boxing) is actually converting basic types into packages class, such as turning double into Double. Boxing is divided into manual boxing and automatic boxing (Auto Boxing). If we manually instantiate a packaging class, this is manual boxing. Before JDK 1.5, manual boxing had to be performed; after JDK 1.5, automatic boxing can be performed without manual operations, which simplifies development and provides convenience.

    2. Case

    The following case is the implementation process of manual boxing and automatic boxing. Before and after JDK 1.5, boxing can be divided into manual boxing and automatic boxing, so everyone should pay attention to it.

    public class BoxTest {
        public static void main(String[] args) {
    	//JDK 1.5之前的拆装箱过程--手动拆装箱
    	byte b = 10;
    	//手动装箱
    	Byte b1 = Byte.valueOf(b);
    	System.out.println("手动装箱后的结果,b1="+b1);
            //手动装箱的另一种形式
    	int m = 100;
            Integer obj = new Integer(m);  
            //手动拆箱的另一种形式
            int n = obj.intValue(); 
            System.out.println("n = " + n);
    	//JDK 1.5之后的拆装箱过程--自动拆装箱        
    	int i = 10;
    	//自动装箱
    	Integer i001 = i;
    	System.out.println("自动装箱后的结果,i001="+i001);
        }
    }

    2. Unboxing

    1. Concept

    The so-called unboxing (Unboxing) is actually converting the packaging type into a basic type, such as turning Double into double . Similarly, unboxing is also divided into manual unboxing and automatic unboxing (Auto Boxing). Manual instantiation of the packaging class is manual unboxing. Before JDK 1.5, unboxing must be done manually, but after JDK 1.5, unboxing can be done automatically.

    2. Case

    The following case is the implementation process of manual unboxing and automatic unboxing. Before and after JDK 1.5, unboxing can be divided into manual unboxing and automatic unboxing, so everyone should pay attention to it.

    public class BoxTest {
        public static void main(String[] args) {
    	//JDK 1.5之前的拆装箱过程--手动拆装箱
    	byte b = 10;
    	//手动装箱
    	Byte b1 = Byte.valueOf(b);
    	System.out.println("手动装箱后的结果,b1="+b1);
    	//手动拆箱
    	byte b2 = b1.byteValue();
    	System.out.println("手动拆箱后的结果="+b2);
    	//JDK 1.5之后的拆装箱过程--自动拆装箱        
    	int i = 10;
    	//自动装箱
    	Integer i001 = i;
    	System.out.println("自动装箱后的结果,i001="+i001);
    	//自动拆箱
    	int i002 = i001;
    	System.out.println("自动拆箱后的结果,i002="+i002);
        }
    }

    3. Summary

    We should note that automatic boxing and automatic unboxing only occur during the compilation phase, and their purpose is to reduce code writing. From a performance point of view, the process of boxing and unboxing will affect the execution efficiency of the code, because the compiled class code strictly distinguishes between basic types and reference types, so the boxing and unboxing during the compilation phase still need to be performed after compilation. Back to the original. In addition, during automatic unboxing, if the assignment is improper, a NullPointerException may occur, as shown below:

    Integer n = null;
    //这里就会产生NPE空指针异常
    int m = n;

    The above is the detailed content of How to correctly do boxing and unboxing of wrapper classes in Java?. For more information, please follow other related articles on the PHP Chinese website!

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