Understanding Java Wrapper Classes: Types, Conversions, and Benefits
Java Wrapper Class
In this tutorial, we will learn about the Java Wrapper class with the help of examples.
Java wrapper classes convert primitive types (int, char, float, etc.) into their corresponding object representations.
Each of the eight primitive types has a dedicated wrapper class.
| Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
boolean | Boolean |
char | Character |
double | Double |
float | Float |
int | Integer |
long | Long |
short | Short |
Convert Primitive Type to Wrapper Objects
Use the valueOf() method to explicitly transform a primitive into its wrapper object.
Example 1: Primitive Types to Wrapper Objects
class Main {
public static void main(String[] args) {
int a = 5;
double b = 5.65;
Integer aObj = Integer.valueOf(a);
Double bObj = Double.valueOf(b);
if(aObj instanceof Integer) {
System.out.println("An object of Integer is created.");
}
if(bObj instanceof Double) {
System.out.println("An object of Double is created.");
}
}
}
Output
An object of Integer is created. An object of Double is created.
The code above demonstrates explicit conversion via valueOf(). Java also supports auto‑boxing, where primitives are implicitly wrapped:
int a = 5;
Integer aObj = a;
double b = 5.6;
Double bObj = b;
For more on autoboxing and unboxing, see our dedicated guide.
Note: Prior to Java 9, wrapper objects could also be created via constructors (e.g., new Integer(a)), but this approach is deprecated.
Wrapper Objects into Primitive Types
To retrieve the underlying primitive, call the corresponding …Value() method on the wrapper object.
Example 2: Wrapper Objects into Primitive Types
class Main {
public static void main(String[] args) {
Integer aObj = Integer.valueOf(23);
Double bObj = Double.valueOf(5.55);
int a = aObj.intValue();
double b = bObj.doubleValue();
System.out.println("The value of a: " + a);
System.out.println("The value of b: " + b);
}
}
Output
The value of a: 23 The value of b: 5.55
Java also performs unboxing automatically:
Integer aObj = Integer.valueOf(2);
int a = aObj;
Double bObj = Double.valueOf(5.55);
double b = bObj;
See the autoboxing and unboxing article for deeper insights.
Advantages of Wrapper Classes
- When collections require objects, wrapper classes bridge the gap between primitives and objects.
// error ArrayList<int> list = new ArrayList<>(); // runs perfectly ArrayList<Integer> list = new ArrayList<>(); - Wrapper objects can hold
null, allowing for nullable references.// generates an error int a = null; // runs perfectly Integer a = null;
Note: Primitive types are generally more efficient than their object counterparts; use primitives when performance is critical.
Java
- Understanding Java’s final Keyword: Variables, Methods, and Classes
- Java instanceof Operator: A Comprehensive Guide
- Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
- Mastering Java’s ObjectInputStream: A Comprehensive Guide
- Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
- Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
- Mastering Java Reader Class: Subclasses, Methods, and Practical Example
- Mastering Java Generics – Building Reusable, Type‑Safe Code
- Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion
- Understanding Java Wrapper Classes: Types, Conversions, and Benefits