Java Wrapper Classes
What are Java Wrapper Classes?
In Java, wrapper classes allow primitive data types (such as int, boolean, char, etc.) to be treated as objects. This is essential when working with APIs and frameworks that require objects instead of primitive values.
Each primitive type in Java has a corresponding wrapper class in the java.lang package.
Why Use Wrapper Classes?
Wrapper classes are commonly used in scenarios where objects are required, such as:
- Working with Java Collections Framework (ArrayList, HashMap, etc.)
- Performing type conversions
- Utilizing utility methods provided by wrapper classes
Example: Using Wrapper Classes in Collections
import java.util.ArrayList;
public class WrapperCollectionDemo {
public static void main(String[] args) {
// Invalid: Collections cannot store primitive types
// ArrayList<int> numbers = new ArrayList<>();
// Valid: Using wrapper class Integer
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println("Numbers: " + numbers);
}
}
Creating Wrapper Objects (Autoboxing)
Java automatically converts primitive types into wrapper objects — a feature known as autoboxing.
Example: Autoboxing in Action
public class AutoboxingExample {
public static void main(String[] args) {
Integer count = 50; // int → Integer
Double price = 199.99; // double → Double
Character grade = 'A'; // char → Character
System.out.println("Count: " + count);
System.out.println("Price: " + price);
System.out.println("Grade: " + grade);
}
}
Extracting Primitive Values (Unboxing)
Wrapper objects can be converted back to primitive types using built-in methods.
Example: Unboxing Values
public class UnboxingExample {
public static void main(String[] args) {
Integer total = 120;
Double discount = 15.5;
Boolean isActive = true;
int totalValue = total.intValue();
double discountValue = discount.doubleValue();
boolean status = isActive.booleanValue();
System.out.println("Total: " + totalValue);
System.out.println("Discount: " + discountValue);
System.out.println("Active: " + status);
}
}
Converting Wrapper Objects to Strings
Wrapper classes provide the toString() method to convert values into string format. This is useful when performing string operations.
Example: Convert Integer to String
public class WrapperToString {
public static void main(String[] args) {
Integer orderId = 9876;
String orderString = orderId.toString();
System.out.println("Order ID length: " + orderString.length());
}
}
Additional Useful Methods
Wrapper classes include several utility methods for data handling:
parseInt(String) → Converts string to integer
valueOf() → Converts primitive or string to wrapper object
compareTo() → Compares two wrapper objects
equals() → Checks value equality
Example: Parsing String to Integer
public class ParsingExample {
public static void main(String[] args) {
String input = "250";
int number = Integer.parseInt(input);
System.out.println("Parsed number: " + number);
}
}
Key Advantages of Wrapper Classes
- Enable use of primitives in object-based collections
- Provide utility methods for conversion and comparison
- Support autoboxing and unboxing for cleaner code
- Improve flexibility and compatibility with Java APIs