Java Autoboxing and Auto-unboxing

Autoboxing and auto-unboxing are features in Java that automatically convert between primitive data types and their corresponding wrapper classes. These features were introduced in Java 5 to simplify the process of working with primitive types and their wrapper classes, such as int and Integer, double and Double, etc.


Autoboxing is the process of automatically converting a primitive type to its corresponding wrapper class when an object of that wrapper class is required. This conversion happens implicitly by the compiler.

// Autoboxing example
int primitiveInt = 42;

// Autoboxing: converting primitive int to Integer wrapper class
Integer wrapperInt = primitiveInt;

System.out.println(wrapperInt); // Output: 42

In the example above, the int value is automatically converted to an Integer object during assignment to wrapperInt.


Auto-unboxing is the opposite process, where the compiler automatically converts an object of a wrapper class to its corresponding primitive type when a primitive type is required.

// Auto-unboxing example
Integer wrapperInt = 42;

// Auto-unboxing: converting Integer wrapper class to primitive int
int primitiveInt = wrapperInt;

System.out.println(primitiveInt); // Output: 42

In this example, the Integer object is automatically converted to an int primitive type during assignment to primitiveInt.

Autoboxing and Auto-unboxing in Collections:

One of the common use cases for autoboxing and auto-unboxing is when working with collections, as collections in Java typically store objects. Autoboxing allows you to add primitive values directly to collections without manually converting them to wrapper classes.

// Autoboxing and Auto-unboxing in Collections
List<Integer> integerList = new ArrayList<>();

// Autoboxing: primitive int to Integer wrapper class

// Auto-unboxing: Integer wrapper class to primitive int
int retrievedValue = integerList.get(0);

System.out.println(retrievedValue); // Output: 1

In this example, the List is declared to store Integer objects, and autoboxing and auto-unboxing happen automatically when adding and retrieving values from the list.

It’s important to note that while autoboxing and auto-unboxing provide convenience, they come with some performance considerations. In certain situations, manual conversion between primitive types and wrapper classes may be preferred for better control over the code.

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object

Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed. There is no need to call a method such as intValue( ) or doubleValue( )

The addition of autoboxing and auto-unboxing greatly streamlines the coding of several algorithms, removing the tedium of manually boxing and unboxing values. It also helps prevent errors. Moreover, it is very important to generics, which operates only on objects. Finally, autoboxing makes working with the Collections Framework

With autoboxing it is no longer necessary to manually construct an object in order to wrap a primitive type. we need only assign that value to a type-wrapper reference. Java automatically constructs the object for us. For example, here is the modern way to construct an Integer object that has the value 100

int i = iOb; // auto-unbox

Example autoboxing/unboxing

class AutoBox {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb); // displays 100 100

Autoboxing and Methods

In addition to the simple case of assignments, autoboxing automatically occurs whenever a primitive type must be converted into an object; auto-unboxing takes place whenever an object must be converted into a primitive type. Thus, autoboxing/unboxing might occur when an argument is passed to a method, or when a value is returned by a method. For example, consider this example

// Autoboxing/unboxing takes place with
// method parameters and return values.
class AutoBox2 {
// Take an Integer parameter and return
// an int value;
static int m(Integer v) {
return v ; // auto-unbox to int
public static void main(String args[]) {
// Pass an int to m() and assign the return value
// to an Integer. Here, the argument 100 is autoboxed
// into an Integer. The return value is also autoboxed
// into an Integer.
Integer iOb = m(100);

Autoboxing/Unboxing Occurs in Expressions

In general, autoboxing and unboxing take place whenever a conversion into an object or from an object is required. This applies to expressions. Within an expression, a numeric object is automatically unboxed. The outcome of the expression is reboxed, if necessary. For example, consider the following program:

Autoboxing/unboxing occurs inside expressions

class AutoBox3 {
public static void main(String args[]) {
Integer iOb, iOb2;
int i;
iOb = 100;
System.out.println("Original value of iOb: " + iOb);
// The following automatically unboxes iOb,
// performs the increment, and then reboxes
// the result back into iOb.
System.out.println("After ++iOb: " + iOb);
// Here, iOb is unboxed, the expression is
// evaluated, and the result is reboxed and
// assigned to iOb2.
iOb2 = iOb + (iOb / 3);
System.out.println("iOb2 after expression: " + iOb2);
// The same expression is evaluated, but the
// result is not reboxed.
i = iOb + (iOb / 3);
System.out.println("i after expression: " + i);


Original value of iOb: 100
After ++iOb: 101
iOb2 after expression: 134
i after expression: 134

Autoboxing/Unboxing Boolean and Character Values


class AutoBox5 {
public static void main(String args[]) {
// Autobox/unbox a boolean.
Boolean b = true;
// Below, b is auto-unboxed when used in
// a conditional expression, such as an if.
if(b) System.out.println("b is true");
// Autobox/unbox a char.
Character ch = 'x'; // box a char
char ch2 = ch; // unbox a char
System.out.println("ch2 is " + ch2);


b is true
ch2 is x

Autoboxing/Unboxing Helps Prevent Errors


// An error produced by manual unboxing.
class UnboxingError {
public static void main(String args[]) {
Integer iOb = 1000; // autobox the value 1000
int i = iOb.byteValue(); // manually unbox as byte !!!
System.out.println(i); // does not display 1000 !

A Word of Warning

autoboxing and auto-unboxing, some might be tempted to use objects such as Integer or Double exclusively, abandoning primitives altogether. For example, with autoboxing/unboxing it is possible to write code like this

// A bad use of autoboxing/unboxing!
Double a, b, c;
a = 10.0;
b = 4.0;
c = Math.sqrt(a*a + b*b);
System.out.println("Hypotenuse is " + c);

Leave a Comment