Member-only story
Autoboxing and Unboxing in Java: A Simple Guide for Beginners
Learn what autoboxing and unboxing are in Java, why they matter, and how they simplify working with primitive types and wrapper classes. Includes simple examples.
2 min readApr 4, 2025
What Are Autoboxing and Unboxing?
In Java, primitive types like int
, double
, and boolean
are not objects — they are basic types.
Java also provides wrapper classes like Integer
, Double
, and Boolean
that wrap these primitives in objects.
Autoboxing:
Automatic conversion of a primitive into its wrapper class.
int num = 5;
Integer obj = num; // Autoboxing: int → Integer
🔹 Unboxing:
Automatic conversion of a wrapper class object back to a primitive.
Integer obj = 10;
int num = obj; // Unboxing: Integer → int
🔧 Why Do We Need It?
Before Java 5, you had to do this manually:
int num = 5;
Integer obj = Integer.valueOf(num); // Manual boxing
int result = obj.intValue(); // Manual unboxing