Autoboxing & Unboxing in Java
In Java, autoboxing and unboxing are automatic conversions between primitive data types and their corresponding wrapper classes. These features were introduced in Java 5 to simplify coding when working with collections, generics, and object-oriented programming.
1. What is Autoboxing?
Autoboxing is the automatic conversion of a primitive data type into its corresponding wrapper class by the Java compiler.
Example of Autoboxing
public class AutoboxingExample {
public static void main(String[] args) {
int primitiveInt = 10;
// Autoboxing: int → Integer
Integer objectInt = primitiveInt;
System.out.println("Primitive int: " + primitiveInt);
System.out.println("Autoboxed Integer: " + objectInt);
}
}
Where Autoboxing is Useful?
- When adding primitive values to collections
- When using generic methods with primitives
import java.util.ArrayList;
public class AutoboxingExample {
public static <T> void printValue(T value) {
System.out.println("Value: " + value);
}
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5); // Autoboxing: int → Integer
numbers.add(10);
System.out.println("Numbers List: " + numbers);
printValue(20); // Autoboxing: int → Integer
printValue(3.14); // Autoboxing: double → Double
}
}
2. What is Unboxing?
Unboxing is the automatic conversion of a wrapper class object back into its corresponding primitive type.
Example of Unboxing
public class UnboxingExample {
public static void main(String[] args) {
Integer objectInt = 20;
// Unboxing: Integer → int
int primitiveInt = objectInt;
System.out.println("Wrapper Integer: " + objectInt);
System.out.println("Unboxed int: " + primitiveInt);
}
}
Where Unboxing is Useful?
- When performing arithmetic operations on wrapper objects
- When passing wrapper objects to methods expecting primitives
public class UnboxingExample {
public static void printSquare(int num) {
System.out.println("Square: " + (num * num));
}
public static void main(String[] args) {
Integer num1 = 50;
Integer num2 = 30;
// Unboxing happens automatically
int sum = num1 + num2;
System.out.println("Sum: " + sum);
Integer objNum = 7;
printSquare(objNum); // Unboxing: Integer → int
}
}
3. Performance Considerations
While autoboxing and unboxing simplify code, they introduce performance overhead:
-
Memory Usage
- Wrapper objects consume more memory than primitives.
- Example:
inttakes 4 bytes, butIntegertakes 16 bytes (due to object overhead).
-
Processing Time
- Unnecessary boxing/unboxing can slow down performance in loops.
- Example:
public class PerformanceTest { public static void main(String[] args) { long start = System.nanoTime(); Integer sum = 0; // Autoboxing for (int i = 0; i < 1000000; i++) { sum += i; // Unboxing & Autoboxing happening repeatedly } long end = System.nanoTime(); System.out.println("Time taken: " + (end - start) + " ns"); } } - Fix: Use
intinstead ofIntegerinside loops.
4. Summary
Autoboxing & unboxing make Java easier to work with, but understanding when and how they occur helps optimize performance.
| Feature | Autoboxing | Unboxing |
|---|---|---|
| Definition | Converts primitive → Wrapper object | Converts Wrapper object → primitive |
| When? | Assigning a primitive value to a wrapper class | Assigning a wrapper object to a primitive type |
| Benefit | Simplifies working with collections, generics | Allows seamless arithmetic operations |
| Downside | Consume more memory and implicit conversions may impact efficiency | |