Autoboxing and Unboxing in JAVA

Autoboxing and Unboxing in JAVA

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

Unboxing is the automatic conversion that the Java compiler makes between an object of a wrapper type to its corresponding primitive.

To know more about Wrapper Classes and Autoboxing, visit the following link: Wrapper classes.

import java.util.List;
import java.util.ArrayList;

public class AutoboxingAndUnboxing {

    public static void main(String args[]) {
        List list = new ArrayList<>();

        int i = 2;
        
        //Autoboxing through method invocation
        list.add(i);

        //Autoboxing through assignment
        Integer iI = i;

        //Unboxing through assignment
        int i2 = iI;

        //Unboxing through method invocation
        unboxing(iI);

    }

    private static void unboxing(int i){
      System.out.println(i);
    }

}