Java Wrapper Classes
In Java, primitive data types (int, double, char, etc.) are efficient but cannot be used directly in collections like ArrayList or in generics. To solve this, Java provides wrapper classes—object representations of primitive types.
Wrapper classes allow primitives to be used as objects, with useful methods for conversions and utilities.
In this guide, we’ll cover:
- What wrapper classes are
- List of wrapper classes in Java
- Autoboxing and unboxing
- Examples with code
- Best practices
What Are Wrapper Classes?
A wrapper class is a class that encapsulates (wraps) a primitive data type into an object.
For example:
• int → Integer
• double → Double
• boolean → Boolean
This allows primitives to work with collections, generics, and APIs that require objects.
List of Wrapper Classes in Java
Primitive Type | Wrapper Class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Example: Using Wrapper Classes
public class WrapperExample {
public static void main(String[] args) {
// Primitive
int primitive = 10;
// Wrapped object
Integer wrapper = Integer.valueOf(primitive);
System.out.println(“Primitive: ” + primitive);
System.out.println(“Wrapper: ” + wrapper);
// Wrapper methods
System.out.println(“Binary of 10: ” + Integer.toBinaryString(10));
}
}
Output:
Primitive: 10
Wrapper: 10
Binary of 10: 1010
Autoboxing (Primitive → Wrapper)
Java automatically converts a primitive into its wrapper object.
Example:
public class AutoBoxingExample {
public static void main(String[] args) {
int num = 5;
Integer obj = num; // autoboxing
System.out.println(“Autoboxed value: ” + obj);
}
}
Unboxing (Wrapper → Primitive)
Java automatically converts a wrapper object back into a primitive.
Example:
public class UnboxingExample {
public static void main(String[] args) {
Integer obj = 20;
int num = obj; // unboxing
System.out.println(“Unboxed value: ” + num);
}
}
Wrapper Classes in Collections
Collections like ArrayList work only with objects, not primitives.
Example:
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
// Autoboxing
numbers.add(10);
numbers.add(20);
// Unboxing
int sum = numbers.get(0) + numbers.get(1);
System.out.println(“Numbers: ” + numbers);
System.out.println(“Sum: ” + sum);
}
}
Output:
Numbers: [10, 20]
Sum: 30
Advantages of Wrapper Classes
- Allow primitives to work with collections and generics.
- Provide utility methods (e.g., Integer.parseInt, Double.valueOf).
- Useful for null handling (unlike primitives).
- Enable object-oriented features with primitive values.
Best Practices
- Prefer primitives for performance-critical code.
- Use wrapper classes when working with collections, generics, or APIs.
- Be cautious with null values (can cause NullPointerException when unboxing).
- Use methods like Integer.valueOf() instead of new Integer() (deprecated).
Conclusion
Java Wrapper Classes bridge the gap between primitive types and objects.
- Use autoboxing to automatically wrap primitives.
- Use unboxing to extract values back to primitives.
- Wrapper classes are essential for collections, generics, and APIs.
By mastering wrapper classes, you can write Java code that is both efficient and object-oriented.