Wrapper Classes in Java
Introduction
Wrapper classes in Java are classes that convert primitive data types into objects.
Java provides a wrapper class for each primitive data type.
For example:
int → Integer double → Double char → Character boolean → Boolean
Wrapper classes are available mainly in the java.lang package, so they do not need to be imported.
Primitive Types and Wrapper Classes
| Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Why Use Wrapper Classes?
Wrapper classes are useful because:
- Collections require objects instead of primitive types.
- They provide useful methods for conversion and comparison.
- They allow primitive values to be treated as objects.
- They support
nullvalues. - They are useful in Generics.
Creating Wrapper Objects
public class WrapperExample { public static void main(String[] args) { Integer number = 100; Double price = 50.5; Character grade = 'A'; Boolean status = true; System.out.println(number); System.out.println(price); System.out.println(grade); System.out.println(status); } }
Output
100 50.5 A true
Autoboxing
Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper object.
Example:
int number = 100; Integer obj = number;
Java automatically converts:
int → Integer
Example
public class AutoboxingExample { public static void main(String[] args) { int number = 50; Integer obj = number; System.out.println(obj); } }
Output
50
Unboxing
Unboxing is the automatic conversion of a wrapper object back into its corresponding primitive type.
Example:
Integer obj = 100; int number = obj;
Java automatically converts:
Integer → int
Example
public class UnboxingExample { public static void main(String[] args) { Integer obj = 100; int number = obj; System.out.println(number); } }
Output
100
Autoboxing and Unboxing Example
public class BoxingExample { public static void main(String[] args) { int a = 10; // Autoboxing Integer obj = a; // Unboxing int b = obj; System.out.println(obj); System.out.println(b); } }
Output
10 10
Wrapper Classes with Collections
Java Collections cannot directly store primitive types.
For example, this is not valid:
// Not allowed ArrayList<int> numbers;
Instead, use the wrapper class:
ArrayList<Integer> numbers = new ArrayList<>();
Example
import java.util.*; public class WrapperCollection { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers); } }
Output
[10, 20, 30]
Here, Java automatically performs autoboxing when primitive int values are added to the ArrayList<Integer>.
Useful Wrapper Class Methods
Wrapper classes provide useful methods for conversion and checking values.
Integer
int number = Integer.parseInt("100"); System.out.println(number);
Output:
100
Double
double price = Double.parseDouble("99.50"); System.out.println(price);
Output:
99.5
Boolean
boolean result = Boolean.parseBoolean("true"); System.out.println(result);
Output:
true
Common Methods
| Method | Purpose |
|---|---|
parseInt() | Converts String to int |
parseDouble() | Converts String to double |
parseFloat() | Converts String to float |
parseLong() | Converts String to long |
toString() | Converts a value to String |
compareTo() | Compares two wrapper objects |
Primitive vs Wrapper Class
| Primitive Type | Wrapper Class |
|---|---|
int | Integer |
| Stores a simple value | Stores a value as an object |
Cannot be null | Can be null |
| Used for basic operations | Used when an object is required |
| Faster for simple operations | Has additional methods |
Real-Life Example
Suppose a student has an age:
int age = 20;
If the age needs to be stored inside a collection:
ArrayList<Integer> ages = new ArrayList<>(); ages.add(age);
Java automatically performs:
int → Integer
This is autoboxing.
Advantages of Wrapper Classes
- Convert primitive values into objects.
- Support Java Collections and Generics.
- Provide useful conversion methods.
- Support
nullvalues. - Provide methods for comparison and manipulation.
- Enable automatic boxing and unboxing.
Important Points
- Wrapper classes represent primitive data types as objects.
Integeris the wrapper class forint.Doubleis the wrapper class fordouble.Characteris the wrapper class forchar.- Autoboxing converts primitive → wrapper object.
- Unboxing converts wrapper object → primitive.
- Wrapper classes are especially important when working with Collections and Generics.
- Wrapper classes are mainly located in the
java.langpackage.
Conclusion
Wrapper classes allow Java primitive values to be used as objects. They are especially important when working with Collections, Generics, and object-based operations. Autoboxing and unboxing make the conversion between primitive types and wrapper objects automatic.