Java Wrapper Classes
Java wrapper classes are object versions of primitive types. They let values like int, double, and boolean be used where Java needs an object.
Wrapper classes also provide helpful methods for converting strings, comparing values, and checking characters.
Primitive Types And Wrapper Classes
Each primitive type has a matching wrapper class. The wrapper class name usually starts with an uppercase letter.
| Primitive type | Wrapper class |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
Most wrapper class names match the primitive type with an uppercase first letter. The two names to remember are int to Integer and char to Character.
Autoboxing And Unboxing
Converting a primitive value to its wrapper object is called boxing. Java usually does this automatically, so it is called autoboxing.
Converting a wrapper object back to a primitive value is called unboxing. Java can do this automatically too.
public class Main {
public static void main(String[] args) {
int primitiveScore = 95;
Integer wrappedScore = primitiveScore;
int scoreAgain = wrappedScore;
Double price = 19.99;
double total = price + 5.00;
System.out.println("Wrapped score: " + wrappedScore);
System.out.println("Score again: " + scoreAgain);
System.out.println("Total: " + total);
}
}
Output:
Wrapped score: 95
Score again: 95
Total: 24.99
How It Works
The assignment Integer wrappedScore = primitiveScore; stores the int value inside an Integer object. Java boxes the value automatically.
The assignment int scoreAgain = wrappedScore; extracts the primitive int value from the wrapper object. Java unboxes the value automatically.
In double total = price + 5.00;, Java unboxes the Double object before doing the addition.
Useful Wrapper Methods
Wrapper classes are useful because they include methods that primitive types do not have. For example, Integer.parseInt() converts text to an int, and Double.parseDouble() converts text to a double.
public class Main {
public static void main(String[] args) {
String countText = "42";
String priceText = "7.50";
char firstLetter = 'J';
int count = Integer.parseInt(countText);
double price = Double.parseDouble(priceText);
boolean isLetter = Character.isLetter(firstLetter);
System.out.println("Count plus one: " + (count + 1));
System.out.println("Price doubled: " + (price * 2));
System.out.println("Is letter: " + isLetter);
}
}
Output:
Count plus one: 43
Price doubled: 15.0
Is letter: true
Without parsing, "42" is text, not a number. After Integer.parseInt(countText), Java can use the value in arithmetic.
When You Need Wrapper Classes
- Use wrapper classes when an API requires objects instead of primitive values.
- Use wrapper methods to convert strings into numbers or booleans.
- Use wrapper constants such as
Integer.MAX_VALUEwhen you need type limits. - Prefer primitive types for ordinary numeric and boolean variables unless you need an object.
Be Careful With Null
A wrapper variable can hold null, but a primitive variable cannot. If Java tries to unbox null, the program will fail at runtime.
For beginner code, keep wrapper variables assigned to real values before using them in calculations.
Takeaway: wrapper classes let primitive values act like objects and give you useful methods such as Integer.parseInt(), but primitive types are still best for simple everyday values.
