Wrapper Classes in Java
Introduction
In Java, Wrapper Classes are used to convert primitive data types into objects. These classes provide a way to use primitive data types as objects, which is essential when working with collections, synchronization, and generics in Java.
Java provides a wrapper class for each primitive data type:
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 Support: Java collections (like
ArrayList
,HashMap
) work only with objects, not primitives. - Utility Methods: Wrapper classes provide useful methods for parsing, converting, and manipulating data.
- Serialization: Objects are required for serialization.
- Multithreading: Synchronization mechanisms require objects.
- Generics Compatibility: Generic types do not support primitive data types.
Creating Wrapper Class Objects
There are two ways to create an object of a wrapper class:
1. Using Constructors (Deprecated since Java 9)
Integer num1 = new Integer(10); // Deprecated
Double num2 = new Double(20.5);
2. Using ValueOf() Method (Preferred)
Integer num1 = Integer.valueOf(10);
Double num2 = Double.valueOf(20.5);
Autoboxing and Unboxing
What is Autoboxing?
Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper class.
int num = 100;
Integer obj = num; // Autoboxing
What is Unboxing?
Unboxing is the automatic conversion of a wrapper class object into its corresponding primitive type.
Integer obj = Integer.valueOf(50);
int num = obj; // Unboxing
Example of Autoboxing & Unboxing:
public class WrapperExample {
public static void main(String[] args) {
Integer obj = 100; // Autoboxing
int num = obj; // Unboxing
System.out.println("Wrapper Object: " + obj);
System.out.println("Primitive Value: " + num);
}
}
Commonly Used Methods in Wrapper Classes
Each wrapper class provides several utility methods. Some commonly used ones include:
1. Parsing Strings to Primitive Types
int num = Integer.parseInt("123");
double value = Double.parseDouble("45.67");
boolean flag = Boolean.parseBoolean("true");
2. Converting Primitives to Strings
String str = Integer.toString(100);
String str2 = Double.toString(20.5);
3. Finding Min/Max Values
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
4. Comparing Wrapper Objects
Integer a = 10;
Integer b = 20;
System.out.println(a.compareTo(b)); // Returns -1 because a < b
Wrapper Classes in Collections
Since Java collections store only objects, wrapper classes allow primitives to be stored inside collections.
import java.util.ArrayList;
public class WrapperInCollections {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // Autoboxing
list.add(20);
int num = list.get(0); // Unboxing
System.out.println("First Element: " + num);
}
}
Performance Considerations
While wrapper classes offer benefits, they also introduce memory overhead due to object creation. Consider using primitive types when performance is critical.
For example, the following code creates multiple objects, increasing memory consumption:
Integer sum = 0;
for (int i = 0; i < 10000; i++) {
sum += i; // Autoboxing and Unboxing occur repeatedly
}
Solution:
Use int
instead of Integer
to avoid unnecessary object creation.
Conclusion
Wrapper classes bridge the gap between primitive data types and object-oriented programming in Java. They provide essential utility methods and enable primitives to be used in collections, synchronization, and generics. However, developers should be mindful of performance implications and use primitives where necessary.
By understanding autoboxing, unboxing, and performance trade-offs, developers can make efficient use of wrapper classes in Java applications.
Sign up here with your email
ConversionConversion EmoticonEmoticon