Boxing

This lesson discusses the concept of Boxing in Java.

We'll cover the following...

Question # 1

What is boxing or autoboxing?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Observe the following code snippet:

Boxing Example

        List<Integer> nums = new ArrayList<>();

        for (int i = 0; i < 100; i++)
            nums.add(i);

The nums variable is a list of Integer objects but we are trying to add() primitive int types to it. Behind the scenes, the compiler automatically creates an object of Integer type and adds it to the list. The above code would be equivalent to the ...