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.
The process of converting a primitive type to its corresponding wrapper class automatically. Here are a couple of examples:
// 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);
// Autoboxing: converting double to Double double primitiveDouble = 3.14; Double wrappedDouble = primitiveDouble; // Autoboxing
Here, the primitive
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);
The process of converting a wrapper class object to its corresponding primitive type automatically.
Here are a couple of examples:
// Unboxing: converting Integer to int Integer wrappedInt = 20; int primitiveInt = wrappedInt; // Unboxing
In this example, the
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 flag = true; // Auto-boxing boolean isFlag = flag; // Auto-unboxing: Boolean to boolean
In this case, the
flag is automatically unboxed to its primitive
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
- Convenience: Autoboxing and auto-unboxing make it more convenient to work with both primitives and objects, reducing the need for explicit conversions.
- Readability: The code becomes more readable and concise as you can use primitives and their corresponding wrapper classes interchangeably.
- Compatibility with Libraries: Many libraries and APIs expect objects, so autoboxing and auto-unboxing help seamlessly integrate with such code.
- 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.
- Potential for NullPointerException: When working with wrapper objects, there's a possibility of encountering a
NullPointerExceptionif the object is
nulland 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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.