Wrapper classes represent primitive data types as objects. These classes are available in the java.lang package and do not require explicit imports.
- Each of the eight primitive data types has a corresponding wrapper class.
- Wrapper classes are immutable, meaning their stored values cannot be modified after creation.
class Geeks{
public static void main(String[] args) {
int b = 357;
// Autoboxing: primitive int -> Integer object
Integer a = b;
System.out.println("The primitive int b is: " + b);
System.out.println("The Integer object a is: " + a);
}
}
Output
The primitive int b is: 357 The Integer object a is: 357
Explanation: Here, the primitive int value is automatically converted into an Integer object by Java. This automatic conversion is called autoboxing.
Primitive Data Types and Wrapper Classes
Primitive Data Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Need for Wrapper Classes
Wrapper classes are useful in the following situations:
- Collections such as ArrayList and HashMap require reference types as generic type arguments.
- Wrapper references can store null to represent missing values.
- Provide utility methods for type conversion, parsing and comparison.
- Allow primitive values to be passed to methods that accept objects.
Autoboxing and Unboxing
Java supports automatic conversion between primitive values and their corresponding wrapper objects.

1. Autoboxing
Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper object.
class Geeks{
public static void main(String[] args){
int num = 25;
// Autoboxing: int to Integer
Integer obj = num;
System.out.println("Primitive value: " + num);
System.out.println("Wrapper object: " + obj);
}
}
Output
Primitive value: 25 Wrapper object: 25
Explanation: Java automatically converts the primitive int value into an Integer object.
2. Unboxing
Unboxing is the automatic conversion of a wrapper object into its corresponding primitive value.
class Geeks{
public static void main(String[] args){
Integer obj = Integer.valueOf(25);
// Unboxing: Integer to int
int num = obj;
System.out.println("Wrapper object: " + obj);
System.out.println("Primitive value: " + num);
}
}
Output
Wrapper object: 25 Primitive value: 25
Explanation: Java automatically extracts the primitive int value from the Integer object.
Common Methods of Wrapper Classes
Wrapper classes provide methods for converting, comparing and processing values.
Method | Description | Example |
|---|---|---|
Converts a string into an int. | Integer.parseInt("100") | |
Returns a wrapper object. | Integer.valueOf(100) | |
Returns a value as an int. | obj.intValue() | |
Returns a string representation. | obj.toString() | |
Compares two values of the same wrapper type. | a.compareTo(b) | |
Checks value equality. | a.equals(b) |
Note: Method availability varies across wrapper classes. Methods such as min(), max(), sum() and decode() are available only in specific wrapper classes.
Important Points About Wrapper Classes
- Wrapper classes are declared final and cannot be extended.
- Unboxing a null reference throws NullPointerException.
- Use equals() instead of == when comparing the values of wrapper objects of the same type.
- Wrapper objects may consume more memory than primitive values.