What is a Wrapper Class in Java?
In Java, wrapper classes are object representations of primitive data types.
They allow primitive values (int, double, char, etc.) to be used as objects.
Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Why Use Wrapper Classes?
- Collections API (e.g.,
ArrayList) can only store objects, not primitives. - Utility Methods — Wrapper classes provide useful methods (e.g., parsing strings to numbers).
- Autoboxing/Unboxing — Automatic conversion between primitives and wrapper objects.
Autoboxing & Unboxing
- Autoboxing: Primitive → Wrapper object (automatic)
- Unboxing: Wrapper object → Primitive (automatic)
Example Program
public class WrapperClassExample {
public static void main(String[] args) {
try {
// Primitive to Wrapper (Autoboxing)
int primitiveInt = 42;
Integer wrapperInt = primitiveInt; // Autoboxing
System.out.println("Wrapper Integer: " + wrapperInt);
// Wrapper to Primitive (Unboxing)
int unboxedInt = wrapperInt; // Unboxing
System.out.println("Unboxed int: " + unboxedInt);
// Using Wrapper Class Methods
String numberStr = "123";
int parsedInt = Integer.parseInt(numberStr); // String → int
System.out.println("Parsed int from String: " + parsedInt);
double parsedDouble = Double.valueOf("45.67"); // String → Double object
System.out.println("Parsed Double object: " + parsedDouble);
// Example with Collections (only objects allowed)
java.util.ArrayList<Integer> numbers = new java.util.ArrayList<>();
numbers.add(10); // Autoboxing from int to Integer
numbers.add(20);
numbers.add(30);
System.out.println("ArrayList of Integers: " + numbers);
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " + e.getMessage());
}
}
}
Key Points in the Code
- Autoboxing:
Integer wrapperInt = primitiveInt; - Unboxing:
int unboxedInt = wrapperInt; - Parsing:
Integer.parseInt()andDouble.valueOf()convert strings to numbers. - Collections:
ArrayList<Integer>stores wrapper objects, not primitives. - Error Handling:
NumberFormatExceptionis caught for invalid string-to-number conversions.
wrapper classes are used to convert primitive data types (e.g., int, double, boolean) into objects. This is particularly useful when working with collections like ArrayList or HashMap, which only accept objects, not primitives. Each primitive type has a corresponding wrapper class, such as Integer for int and Double for double.
Key Features and Benefits
Object Representation of Primitives: Wrapper classes allow primitive types to be treated as objects, enabling their use in object-oriented contexts.
int primitive = 10;
Integer wrapped = Integer.valueOf(primitive); // Converts int to Integer
Null Handling: Unlike primitives, wrapper classes can hold null values, which is useful for representing the absence of data.
Integer age = null; // Represents an unknown age
Utility Methods: Wrapper classes provide methods for type conversion, parsing, and other operations.
String str = "123";
int number = Integer.parseInt(str); // Converts string to int
String text = Integer.toString(456); // Converts int to string
Autoboxing and Unboxing: Java automatically converts between primitives and their wrapper classes. This is known as autoboxing (primitive to wrapper) and unboxing (wrapper to primitive).
int primitive = 10;
Integer wrapped = primitive; // Autoboxing
int unwrapped = wrapped; // Unboxing
Common Use Cases
Collections: Wrapper classes are essential for storing primitive values in collections like ArrayList or HashMap.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10); // Autoboxing converts int to Integer
Null Representation: They allow null to represent missing or undefined values, which is not possible with primitives.
Type Conversion: Wrapper classes simplify converting between strings and numeric types.
String input = "42";
int value = Integer.parseInt(input); // String to int
Important Considerations
Memory Usage: Wrapper classes consume more memory than primitives because they are objects. Avoid using them for large-scale computations or memory-intensive tasks.
NullPointerException: Using null values in wrapper classes can lead to exceptions during unboxing.
Integer value = null;
int primitive = value; // Throws NullPointerException
Equality Comparison: Use .equals() for comparing wrapper objects instead of ==, which checks reference equality.
Integer a = 128;
Integer b = 128;
System.out.println(a.equals(b)); // true
System.out.println(a == b); // false (different objects)