What is Java Wrapper Classes?
In Java, every primitive data type (e.g., int, char, boolean) has a corresponding Wrapper Class that provides an object representation of the primitive type. Wrapper classes are part of the java.lang package, and they enable primitive data types to be used as objects. This is essential when working with frameworks or APIs that require objects rather than primitives, such as collections (ArrayList, HashMap).
What Are Wrapper Classes?
Wrapper classes “wrap” a primitive data type into an object. For example, the int
primitive is wrapped by the Integer class and the double primitive is wrapped by the Double class.
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 |
Why Use Wrapper Classes?
- Object Representation:
Some data structures, like collections, only store objects, not primitives. Example: You cannot store an int directly in an ArrayList—it must be wrapped in an Integer. - Utility Methods:
Wrapper classes provide several utility methods, such as parsing strings into numeric types or converting objects back to primitives. - Autoboxing and Unboxing:
Java automatically converts primitives to objects (autoboxing) and vice versa (unboxing), making it easier to use wrapper classes. - Null Representation:
Unlike primitives, wrapper class objects can be null, making them useful in scenarios where a “no value” state needs to be represented.
Key Features of Wrapper Classes
- Immutability:
Wrapper class objects are immutable, meaning their values cannot be changed once assigned. - Support for Conversion:
Wrapper classes can convert primitives to strings and vice versa.- Primitive to String: Use String.valueOf().
- String to Primitive: Use parsing methods like Integer.parseInt().
Examples of Wrapper Class Usage
1. Using Wrapper Classes with Collections
import java.util.ArrayList;
public class WrapperExample {
public static void main(String[] args) {
// Create an ArrayList to store Integer objects
ArrayList<Integer> numbers = new ArrayList<>();
// Add primitive values (autoboxing occurs)
numbers.add(10); // Autoboxed to Integer(10)
numbers.add(20);
// Retrieve values (unboxing occurs)
int num = numbers.get(0); // Unboxed to int
System.out.println("First number: " + num);
}
}
Output:
First number: 10
2. Conversion Between Primitives and Strings
public class ConversionExample {
public static void main(String[] args) {
// Primitive to String
int number = 100;
String str = String.valueOf(number);
System.out.println("String representation: " + str);
// String to Primitive
String numStr = "200";
int num = Integer.parseInt(numStr);
System.out.println("Integer value: " + num);
}
}
Output:
String representation: 100
Integer value: 200
3. Using Utility Methods
public class UtilityMethods {
public static void main(String[] args) {
// Maximum and minimum values
System.out.println("Max value of Integer: " + Integer.MAX_VALUE);
System.out.println("Min value of Integer: " + Integer.MIN_VALUE);
// Type checking
boolean isDigit = Character.isDigit('9');
System.out.println("Is '9' a digit? " + isDigit);
}
}
Output:
Max value of Integer: 2147483647
Min value of Integer: -2147483648
Is '9' a digit? true
Autoboxing and Unboxing in Java
What is Autoboxing?
Autoboxing automatically converts a primitive value into its corresponding wrapper class object.
Example:
int num = 5;
Integer obj = num; // Autoboxing
What is Unboxing?
Unboxing automatically converts a wrapper class object back to its primitive value.
Example:
Integer obj = 10;
int num = obj; // Unboxing
Important Wrapper Class Methods
Value Conversion:
Convert an object to its primitive value using methods like intValue(), doubleValue(), etc. Example:
Integer obj = 100;
int num = obj.intValue();
Parsing Strings:
Parse strings into primitive types. Example:
int num = Integer.parseInt("123");
Static Utility Methods:
Use methods like MAX_VALUE, MIN_VALUE, isDigit() etc.
Limitations of Wrapper Classes
- Performance Overhead:
Wrapping and unwrapping primitives involves extra processing, which can impact performance in scenarios with large data. - Memory Usage:
Wrapper objects consume more memory compared to primitive types.