Skip to content

Java Autoboxing and Unboxing

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

Java Auto-Boxing and Auto-unboxing are features introduced in Java 5 (JDK 5) to simplify working with primitives and objects.

Wrapper classes are all final, so once created, you cannot alter the underlying value. They are immutable. Autoboxing and unboxing use value copy. There are no shared values or references. Every boxing operation is a copy.

Java also provides a feature called auto-boxing and auto-unboxing. This allows the conversion between primitive types and their corresponding wrapper classes to happen automatically when needed.

Autoboxing

The process of converting a primitive type to its corresponding wrapper class automatically. Here are a couple of examples:

Integer Autoboxing

From int to Integer.

// Autoboxing: converting int to Integer
int primitiveInt = 10;
Integer wrappedInt = primitiveInt; // Autoboxing

Behind the scenes, this is equivalent to explicitly creating an Integer object like this:

Integer wrappedInt = new Integer(primitiveInt); 

Double Autoboxing

// Autoboxing: converting double to Double
double primitiveDouble = 3.14;
Double wrappedDouble = primitiveDouble; // Autoboxing

Here, the primitive double value 3.14 is automatically converted to a Double object. Behind the scenes, this is equivalent to explicitly creating an `Double` object like this:

Double wrappedDouble = new Double(primitiveDouble);

Auto-unboxing

The process of converting a wrapper class object to its corresponding primitive type automatically.

Here are a couple of examples:

Integer Auto-unboxing

From Integer to int.

// Unboxing: converting Integer to int
Integer wrappedInt = 20;
int primitiveInt = wrappedInt; // Unboxing

In this example, the Integer object wrappedInt is automatically unboxed to its primitive int value. Behind the scenes, this is equivalent to explicitly calling the intValue() method like this:

int value = wrappedInt.intValue();

Boolean Auto-unboxing

Boolean flag = true; // Auto-boxing
boolean isFlag = flag; // Auto-unboxing: Boolean to boolean

In this case, the Boolean object flag is automatically unboxed to its primitive boolean value.

These features make it more convenient to work with both primitive types and their wrapper classes in Java.

Auto-boxing and unboxing in collections

Autoboxing and unboxing are particularly useful when working with collections, as collections can only hold objects, not primitives.

// Autoboxing in ArrayList
ArrayList<Integer> intList = new ArrayList<>();
intList.add(30); // Autoboxing: int to Integer

// Unboxing from ArrayList
int value = intList.get(0); // Unboxing: Integer to int

Benefits and Considerations

  1. Convenience: Autoboxing and auto-unboxing make it more convenient to work with both primitives and objects, reducing the need for explicit conversions.
  2. Readability: The code becomes more readable and concise as you can use primitives and their corresponding wrapper classes interchangeably.
  3. Compatibility with Libraries: Many libraries and APIs expect objects, so autoboxing and auto-unboxing help seamlessly integrate with such code.

Caution

  1. Performance Overhead: In situations where performance is critical, be cautious, as autoboxing and auto-unboxing can incur a small performance overhead compared to working directly with primitives.
  2. Potential for NullPointerException: When working with wrapper objects, there's a possibility of encountering a NullPointerException if the object is null and you try to perform auto-unboxing.

Autoboxing and auto-unboxing are powerful features that enhance the flexibility and readability of Java code. However, it's important to use them judiciously, especially in performance-sensitive scenarios, and be mindful of potential pitfalls.

Additional Resources

  1. Java primitive and Non-primitive data types.
  2. How to convert String to int in Java?
  3. How To Convert String To Integer in Java?
  4. What are wrapper classes in Java?
Java

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.