Chapter 50 of 57

Autoboxing and Unboxing in Java

We already know that Java has primitive data types such as int, double, char, and boolean, and their corresponding wrapper classes such as Integer, Double, Character, and Boolean.

Sometimes Java needs an object instead of a primitive value. Fortunately, Java can automatically convert between the two.

This automatic conversion is called autoboxing and unboxing.

What is Autoboxing?

Autoboxing means automatically converting a primitive value into its corresponding wrapper object.

For example:

int number = 10;

Integer value = number;

Here, number is an int, while value is an Integer.

Java automatically converts:

int → Integer

We don't have to manually create an Integer object.

Without autoboxing, we could write:

int number = 10;

Integer value = Integer.valueOf(number);

But Java allows the simpler version:

Integer value = number;

That's autoboxing.

Autoboxing with Collections

One of the most common places you'll see autoboxing is with collections.

As we learned earlier, collections such as ArrayList work with objects, not primitive types.

So we cannot write:

ArrayList<int> numbers = new ArrayList<>(); // Error

Instead, we use Integer:

ArrayList<Integer> numbers = new ArrayList<>();

Now look at this:

int number = 10;

numbers.add(number);

The add() method expects an Integer, but we're giving it an int.

Java automatically converts:

int → Integer

and stores it in the ArrayList.

This automatic conversion is autoboxing.

What is Unboxing?

Unboxing is the opposite of autoboxing.

It means automatically converting a wrapper object back into its corresponding primitive type.

For example:

Integer number = 100;

int value = number;

Java automatically converts:

Integer → int

That's called unboxing.

Without automatic conversion, we could write:

Integer number = 100;

int value = number.intValue();

But Java allows us to simply write:

int value = number;

Autoboxing and Unboxing Together

We can use both in the same program:

int number = 50;

// Autoboxing
Integer wrapped = number;

// Unboxing
int result = wrapped;

System.out.println(result);

Output:

50

So the process looks like:

Primitive
   ↓
Autoboxing
   ↓
Wrapper Object
   ↓
Unboxing
   ↓
Primitive

More Examples

Here are some common conversions:

int a = 10;
Integer b = a;        // Autoboxing

double c = 20.5;
Double d = c;         // Autoboxing

Integer x = 100;
int y = x;            // Unboxing

Double p = 50.5;
double q = p;         // Unboxing

Java automatically handles these conversions.

Autoboxing in Method Calls

Autoboxing can also happen when passing arguments to methods.

For example:

static void printNumber(Integer number) {
    System.out.println(number);
}

Now we can pass an int:

int number = 25;

printNumber(number);

The method expects an Integer, but we provide an int.

Java automatically boxes the int into an Integer.

Unboxing in Calculations

Java can also unbox wrapper objects when performing calculations.

For example:

Integer a = 10;
Integer b = 20;

int result = a + b;

System.out.println(result);

Output:

30

Here, Java automatically converts the Integer objects into int values so that the addition can be performed.

Be Careful with null

There is one important thing to remember.

A wrapper object can contain null, but a primitive cannot.

For example:

Integer number = null;

This is allowed because Integer is an object.

But if Java tries to unbox it:

int value = number;

it will cause a NullPointerException.

So be careful when unboxing wrapper objects that might contain null.

Primitive vs Wrapper

Let's quickly compare them:

Primitive

Wrapper

int

Integer

double

Double

char

Character

boolean

Boolean

Stores a basic value

Represents the value as an object

Cannot be null

Can be null

A Complete Example

Let's see how autoboxing and unboxing work with an ArrayList:

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {

        ArrayList<Integer> numbers = new ArrayList<>();

        int number = 100;

        // Autoboxing
        numbers.add(number);

        // Unboxing
        int firstNumber = numbers.get(0);

        System.out.println(firstNumber);
    }
}

Output:

100

When we write:

numbers.add(number);

Java converts int to Integer.

When we write:

int firstNumber = numbers.get(0);

Java converts Integer back to int.

The main thing to remember is:

Autoboxing → primitive to wrapper object

Unboxing → wrapper object to primitive

Java performs both conversions automatically when needed, which makes working with wrapper classes and collections much easier.