Wrapper Classes & Autoboxing / Unboxing in Java
In Core Java, Wrapper Classes are part of the java.lang package. They are used to convert primitive data types (like int, char, double) into objects, and vice versa.
This conversion allows primitives to work in Java’s Collections Framework and other object-oriented contexts.
What Are Wrapper Classes?
A Wrapper Class “wraps” (or encapsulates) a primitive data type into an object.
Each primitive type in Java has a matching wrapper class:

| Primitive Type | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Why Use Wrapper Classes?
- To use primitive data in collections (like
ArrayList,HashMap) — since collections work only with objects. - To use utility methods like
Integer.parseInt(),Double.valueOf(), etc. - For data conversion between primitives, Strings, and objects.
- Useful in serialization, reflection, and generics.
Example – Primitive to Wrapper (Boxing)
public class Example {
public static void main(String[] args) {
int num = 10; // primitive
Integer obj = Integer.valueOf(num); // manual boxing
System.out.println(obj);
}
}
Output:
10
Here, the primitive int is converted into an Integer object manually using Integer.valueOf().
Autoboxing (Automatic Conversion)
Autoboxing is the automatic conversion of a primitive type to its wrapper class object by the Java compiler.
public class AutoBoxingExample {
public static void main(String[] args) {
int a = 5;
Integer obj = a; // autoboxing
System.out.println("Integer object: " + obj);
}
}
Output:
Integer object: 5
Note : The compiler automatically wraps int a into an Integer object — no need for Integer.valueOf() manually.
Unboxing (Object → Primitive)
Unboxing is the reverse process — converting a wrapper object back into a primitive value.
public class UnboxingExample {
public static void main(String[] args) {
Integer obj = 20; // autoboxing
int num = obj; // unboxing
System.out.println("Primitive int: " + num);
}
}
Output:
Primitive int: 20
The wrapper Integer obj is automatically converted back to int.
Autoboxing & Unboxing Together
import java.util.*;
public class Demo {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
// Autoboxing: int → Integer
list.add(10);
list.add(20);
list.add(30);
// Unboxing: Integer → int
for (int num : list) {
System.out.println(num);
}
}
}
Output:
10
20
30
Note : Java automatically performs both conversions — keeping your code clean and readable.
Useful Wrapper Class Methods
| Method | Description | Example |
|---|---|---|
Integer.parseInt("100") | Converts String → int | int n = Integer.parseInt("100"); |
Integer.valueOf(100) | Returns Integer object | Integer obj = Integer.valueOf(100); |
Double.parseDouble("10.5") | Converts String → double | double d = Double.parseDouble("10.5"); |
Character.isDigit('5') | Checks if a character is a digit | true |
Boolean.parseBoolean("true") | Converts String → boolean | true |
Differences Between Primitive & Wrapper
| Feature | Primitive | Wrapper Class |
|---|---|---|
| Stored In | Stack memory | Heap memory |
| Default Value | 0, false | null |
| Can Be Null | No | Yes |
| Used in Collections | No | Yes |
| Type | Value type | Object type |
| Performance | Faster | Slightly slower (object overhead) |
Common Mistakes / Pitfalls
| Problem | Description |
|---|---|
| NullPointerException | Unboxing a null wrapper object causes an error. |
| Performance Overhead | Frequent boxing/unboxing can reduce speed. |
| Equality Confusion | == compares object references, not values — use .equals() for value comparison. |
Points to Remember
- Wrapper classes make primitive data usable as objects.
- Autoboxing/unboxing simplifies conversion automatically.
- Collections and Generics require objects, not primitives.
- Wrapper objects are immutable — their values cannot be changed once created.
- Use
.equals()to compare wrapper values, not==.
