Java Wrapper Classes
Wrapper classes represent primitive-style values as objects. They let values such as int numbers be used with APIs that work with references, including generic collections. Conversion, null handling, and equality need care because a wrapper is not the same thing as its primitive value.
Sometimes, however, we need to treat a primitive value as an object. Wrapper classes provide object representations of primitive data types.
For example:
int number = 10;Here, number is a primitive value.
The corresponding wrapper class is:
Integer number = 10;Here, Integer is an object type that represents the primitive int.
What Are Wrapper Classes?
A wrapper class is a class that wraps a primitive data type inside an object.
Java provides a wrapper class for each primitive type.
| Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
There is no wrapper class named Void for the void primitive in the same sense, but Java provides java.lang.Void as a special class representing the void type.
All the standard wrapper classes are available in the java.lang package, so they do not need to be explicitly imported.
Why Do We Need Wrapper Classes?
Java has two broad categories of types:
Java Types
│
├── Primitive Types
│ ├── int
│ ├── char
│ ├── double
│ └── ...
│
└── Reference Types
├── Integer
├── Character
├── String
└── ...Primitive types are simple and efficient, but they are not objects.
Wrapper classes are useful when an object is required.
For example, collections such as ArrayList work with objects:
ArrayList<Integer> numbers;We cannot write:
ArrayList<int> numbers;because Java generics do not directly accept primitive types.
Therefore, wrapper classes allow primitive values to be used where objects are required.
Primitive vs Wrapper
Consider:
int a = 10;
Integer b = 10;Here:
a → primitive int
b → Integer object/referenceExample
public class HelloWorld {
public static void main(String[] args) {
int a = 10;
Integer b = 10;
System.out.println(a);
System.out.println(b);
}
}Output
10
10The output is the same, but the types are different.
Wrapper Class Hierarchy
The wrapper classes are classes, so they can participate in Java's object-oriented features.
For example:
Object
|
+-- Number
|
+-- Byte
+-- Short
+-- Integer
+-- Long
+-- Float
+-- DoubleCharacter and Boolean do not extend Number.
Creating Wrapper Objects
There are several ways to obtain wrapper objects.
Using a value directly
Integer number = 100;This commonly involves autoboxing, which is explained later.
Using valueOf()
Integer number = Integer.valueOf(100);Example
public class HelloWorld {
public static void main(String[] args) {
Integer number = Integer.valueOf(100);
System.out.println(number);
}
}Output
100Using valueOf() is generally preferred over manually creating wrapper objects.
Deprecated Wrapper Constructors
Older Java code may contain:
Integer number = new Integer(100);Modern Java code should avoid this style. Wrapper constructors such as new Integer(int) are deprecated for removal. They still exist in Java 25, but new code should use autoboxing or valueOf() instead.
Prefer:
Integer number = Integer.valueOf(100);or:
Integer number = 100;Autoboxing
Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper object.
Example
int number = 10;
Integer wrapper = number;Java automatically converts:
int → IntegerExample
public class HelloWorld {
public static void main(String[] args) {
int number = 10;
Integer wrapper = number;
System.out.println(wrapper);
}
}Output
10Conceptually, Java performs something similar to:
Integer wrapper = Integer.valueOf(number);Unboxing
Unboxing is the automatic conversion of a wrapper object back into its corresponding primitive type.
Example
Integer wrapper = 10;
int number = wrapper;Java automatically converts:
Integer → intExample
public class HelloWorld {
public static void main(String[] args) {
Integer wrapper = 10;
int number = wrapper;
System.out.println(number);
}
}Output
10Conceptually, Java performs something similar to:
int number = wrapper.intValue();Autoboxing and Unboxing Together
Both conversions can happen automatically.
public class HelloWorld {
public static void main(String[] args) {
int a = 10;
Integer b = a; // Autoboxing
int c = b; // Unboxing
System.out.println(c);
}
}Output
10The flow is:
int
↓
Autoboxing
↓
Integer
↓
Unboxing
↓
intWrapper Classes with Collections
One of the most important uses of wrapper classes is with Java collections.
Consider:
ArrayList<Integer> numbers = new ArrayList<>();We cannot write:
ArrayList<int> numbers = new ArrayList<>();because int is a primitive type.
Example
import java.util.ArrayList;
public class HelloWorld {
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]The values are automatically boxed from int to Integer.
Wrapper Classes as Method Parameters
A method can accept a wrapper object.
Example
public class HelloWorld {
static void display(Integer number) {
System.out.println("Number: " + number);
}
public static void main(String[] args) {
int value = 100;
display(value);
}
}Output
Number: 100The primitive int is automatically boxed into Integer.
Wrapper Classes as Return Types
A method can also return a wrapper object.
public class HelloWorld {
static Integer getNumber() {
return 100;
}
public static void main(String[] args) {
Integer number = getNumber();
System.out.println(number);
}
}Output
100Converting String to Wrapper
Wrapper classes provide methods for converting strings into numeric wrapper values.
For example:
String value = "100";
Integer number = Integer.valueOf(value);Example
public class HelloWorld {
public static void main(String[] args) {
String value = "100";
Integer number = Integer.valueOf(value);
System.out.println(number);
}
}Output
100parseInt() vs valueOf()
Integer provides both:
Integer.parseInt()and:
Integer.valueOf()The important difference is their return type.
parseInt()
Returns a primitive int.
int number = Integer.parseInt("100");valueOf()
Returns an Integer.
Integer number = Integer.valueOf("100");Example
public class HelloWorld {
public static void main(String[] args) {
int a = Integer.parseInt("100");
Integer b = Integer.valueOf("200");
System.out.println(a);
System.out.println(b);
}
}Output
100
200Parsing Other Numeric Types
Similar methods exist for other wrapper classes.
byte b = Byte.parseByte("10");
short s = Short.parseShort("20");
int i = Integer.parseInt("30");
long l = Long.parseLong("40");
float f = Float.parseFloat("50.5");
double d = Double.parseDouble("60.5");Example
public class HelloWorld {
public static void main(String[] args) {
int number = Integer.parseInt("123");
double price = Double.parseDouble("99.50");
System.out.println(number);
System.out.println(price);
}
}Output
123
99.5Converting Wrapper to String
Wrapper classes can be converted into strings.
Example
Integer number = 100;
String value = number.toString();Example
public class HelloWorld {
public static void main(String[] args) {
Integer number = 100;
String value = number.toString();
System.out.println(value);
System.out.println(value.getClass().getName());
}
}Output
100
java.lang.StringInteger.toString()
You can also convert an integer directly to a string:
String value = Integer.toString(100);Example
public class HelloWorld {
public static void main(String[] args) {
String value = Integer.toString(100);
System.out.println(value);
}
}Output
100Wrapper Class Constants
Wrapper classes provide useful constants.
For example:
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);Output
2147483647
-2147483648Similarly:
System.out.println(Long.MAX_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Float.MAX_VALUE);Wrapper classes provide constants describing the limits and characteristics of their corresponding types.
Integer.MAX_VALUE
The maximum value of an int is:
Integer.MAX_VALUEExample
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Maximum int: " + Integer.MAX_VALUE);
System.out.println("Minimum int: " + Integer.MIN_VALUE);
}
}Output
Maximum int: 2147483647
Minimum int: -2147483648Wrapper Class Utility Methods
Wrapper classes provide useful methods for working with primitive values and strings.
For example, Integer provides:
Integer.parseInt()
Integer.valueOf()
Integer.toString()
Integer.compare()
Integer.max()
Integer.min()
Integer.sum()Example
public class HelloWorld {
public static void main(String[] args) {
System.out.println(Integer.max(10, 20));
System.out.println(Integer.min(10, 20));
System.out.println(Integer.sum(10, 20));
}
}Output
20
10
30Comparing Wrapper Objects
This is an important topic.
Consider:
Integer a = 100;
Integer b = 100;
System.out.println(a == b);The result may be:
trueBut you should not use == to compare wrapper objects by value.
Use:
equals()instead.
Example
public class HelloWorld {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
System.out.println(a.equals(b));
}
}Output
trueWhy Can == Give Unexpected Results?
Some wrapper classes cache commonly used values.
For Integer, values in a commonly cached range are typically reused, so:
Integer a = 100;
Integer b = 100;
System.out.println(a == b);may print:
trueBut:
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b);may print:
falseThe exact behavior of wrapper caching should not be used as a value-comparison technique.
The correct approach is:
a.equals(b)when both references are non-null.
Wrapper Comparison with compareTo()
Wrapper classes such as Integer implement Comparable.
Example
public class HelloWorld {
public static void main(String[] args) {
Integer a = 10;
Integer b = 20;
System.out.println(a.compareTo(b));
System.out.println(b.compareTo(a));
System.out.println(a.compareTo(10));
}
}Output
-1
1
0The meaning is:
negative → first value is smaller
zero → values are equal
positive → first value is greaterNull Values with Wrapper Classes
Primitive types cannot store null.
This is invalid:
int number = null;But a wrapper reference can:
Integer number = null;Example
public class HelloWorld {
public static void main(String[] args) {
Integer number = null;
System.out.println(number);
}
}Output
nullThis is one important difference between primitives and wrapper references.
NullPointerException During Unboxing
Be careful when unboxing a null wrapper.
Example
public class HelloWorld {
public static void main(String[] args) {
Integer number = null;
int value = number;
System.out.println(value);
}
}This causes a:
NullPointerExceptionWhy?
Because Java effectively needs to call something equivalent to:
number.intValue()But number is null.
Therefore, unboxing a null wrapper reference causes a NullPointerException.
Wrapper Classes and Method Overloading
Wrapper classes can affect method overload selection.
Example
public class HelloWorld {
static void display(int value) {
System.out.println("int");
}
static void display(Integer value) {
System.out.println("Integer");
}
public static void main(String[] args) {
int a = 10;
Integer b = 20;
display(a);
display(b);
}
}Output
int
IntegerThe primitive argument selects the int method, while the wrapper argument selects the Integer method.
Wrapper Classes and Arithmetic
Wrapper objects can participate in arithmetic because Java automatically unboxes them.
Example
public class HelloWorld {
public static void main(String[] args) {
Integer a = 10;
Integer b = 20;
int result = a + b;
System.out.println(result);
}
}Output
30Conceptually:
Integer → int
Integer → int
↓
addition
↓
intWrapper Classes with Generics
Wrapper classes are heavily used with Java generics.
This is valid:
ArrayList<Integer> numbers = new ArrayList<>();This is invalid:
ArrayList<int> numbers = new ArrayList<>();Similarly:
Map<String, Integer> marks = new HashMap<>();Here:
String → reference type
Integer → wrapper/reference typeExample
import java.util.HashMap;
import java.util.Map;
public class HelloWorld {
public static void main(String[] args) {
Map<String, Integer> marks = new HashMap<>();
marks.put("Java", 90);
marks.put("SQL", 85);
System.out.println(marks);
}
}Output
{Java=90, SQL=85}Character Wrapper Class
The wrapper class for char is:
CharacterExample
public class HelloWorld {
public static void main(String[] args) {
Character ch = 'A';
System.out.println(ch);
System.out.println(Character.isLetter(ch));
System.out.println(Character.isDigit(ch));
}
}Output
A
true
falseCharacter provides many useful methods.
Examples:
Character.isLetter()
Character.isDigit()
Character.isWhitespace()
Character.isUpperCase()
Character.isLowerCase()
Character.toUpperCase()
Character.toLowerCase()Boolean Wrapper Class
The wrapper class for boolean is:
BooleanExample
public class HelloWorld {
public static void main(String[] args) {
Boolean value = true;
System.out.println(value);
}
}Output
trueYou can also parse a string:
boolean value = Boolean.parseBoolean("true");Example
public class HelloWorld {
public static void main(String[] args) {
boolean value = Boolean.parseBoolean("true");
System.out.println(value);
}
}Output
trueByte, Short, Long, Float and Double
Java provides wrapper classes for all numeric primitive types.
| Primitive | Wrapper |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Example
Byte byteValue = 10;
Short shortValue = 20;
Integer intValue = 30;
Long longValue = 40L;
Float floatValue = 50.5f;
Double doubleValue = 60.5;Primitive to Wrapper Conversion
You can explicitly use valueOf():
int primitive = 100;
Integer wrapper = Integer.valueOf(primitive);Or rely on autoboxing:
int primitive = 100;
Integer wrapper = primitive;Wrapper to Primitive Conversion
You can explicitly call the corresponding method:
Integer wrapper = 100;
int primitive = wrapper.intValue();Or rely on unboxing:
Integer wrapper = 100;
int primitive = wrapper;Complete Conversion Example
public class HelloWorld {
public static void main(String[] args) {
// Primitive
int primitive = 100;
// Autoboxing
Integer wrapper = primitive;
// Unboxing
int anotherPrimitive = wrapper;
// Wrapper to String
String text = wrapper.toString();
// String to primitive
int parsedValue = Integer.parseInt("500");
// String to wrapper
Integer parsedWrapper = Integer.valueOf("600");
System.out.println(primitive);
System.out.println(wrapper);
System.out.println(anotherPrimitive);
System.out.println(text);
System.out.println(parsedValue);
System.out.println(parsedWrapper);
}
}Output
100
100
100
100
500
600Wrapper Classes in Java Memory
A primitive variable directly stores its value.
For example:
int number = 100;A wrapper variable is a reference to an object representing the value:
Integer number = 100;Conceptually:
Primitive:
number
|
100
Wrapper:
number
|
↓
Integer object
|
100The exact JVM implementation and memory behavior is more detailed than this conceptual model, but the distinction is useful for understanding primitive versus reference types.
Performance Considerations
Primitive values are generally more lightweight than wrapper objects.
For example:
int number = 100;does not require an Integer object.
Whereas:
Integer number = 100;uses a reference type and may involve an object representation.
Therefore, when object behavior is not required, primitives are often preferable for simple numeric computations.
However, wrapper classes are necessary in many APIs, especially:
- Collections
- Generics
- APIs requiring objects
- Nullable values
- Utility methods
Common Mistakes
1. Comparing wrappers using ==
Avoid:
Integer a = 1000;
Integer b = 1000;
if (a == b) {
// ...
}Use:
if (a.equals(b)) {
// ...
}For potentially null values, use a null-safe approach such as:
Objects.equals(a, b)2. Unboxing a null wrapper
Avoid:
Integer number = null;
int value = number;This causes NullPointerException.
3. Using wrapper classes unnecessarily
For simple calculations, this is often unnecessary:
Integer a = 10;
Integer b = 20;
Integer result = a + b;If you do not need an object, a primitive may be simpler:
int a = 10;
int b = 20;
int result = a + b;4. Using deprecated constructors
Avoid old code such as:
new Integer(10);Prefer:
Integer.valueOf(10);or:
Integer number = 10;5. Confusing parseInt() and valueOf()
Remember:
Integer.parseInt("100")returns:
intwhile:
Integer.valueOf("100")returns:
IntegerWrapper Classes Quick Reference
| Wrapper | Common Conversion | Example |
|---|---|---|
Byte | parseByte() | Byte.parseByte("10") |
Short | parseShort() | Short.parseShort("10") |
Integer | parseInt() | Integer.parseInt("10") |
Long | parseLong() | Long.parseLong("10") |
Float | parseFloat() | Float.parseFloat("10.5") |
Double | parseDouble() | Double.parseDouble("10.5") |
Character | Character utility methods | Character.isLetter('A') |
Boolean | parseBoolean() | Boolean.parseBoolean("true") |
Primitive vs Wrapper Class
| Feature | Primitive | Wrapper |
|---|---|---|
| Example | int | Integer |
| Object | No | Yes |
Can store null | No | Yes |
| Works directly with generics | No | Yes |
| Collections | Not directly | Yes |
| Has utility methods | Limited | Yes |
| Autoboxing | N/A | Supports conversion |
| Unboxing | N/A | Supports conversion |
| Generally lightweight | Yes | More overhead |
| Can call methods | No | Yes |
Important Points to Remember
- Every primitive type except
voidhas a corresponding standard wrapper class. int→Integer.char→Character.boolean→Boolean.- Wrapper classes are objects.
- Wrapper references can hold
null. - Autoboxing converts primitive → wrapper.
- Unboxing converts wrapper → primitive.
- Wrapper classes are required when primitives need to be used with Java generics and collections.
- Use
equals()rather than==when comparing wrapper values. - Be careful when unboxing
null. parseInt()returns a primitiveint.valueOf()returns anInteger.- Wrapper classes provide useful constants such as
Integer.MAX_VALUE. - Modern Java code should avoid deprecated wrapper constructors such as
new Integer(10).
Conclusion
Wrapper classes provide object representations of Java's primitive data types.
For example:
int → Integer
long → Long
double → Double
char → Character
boolean → BooleanTheir most important purpose is to allow primitive values to work in situations where Java requires objects, especially collections and generics.
Two important concepts associated with wrapper classes are:
Autoboxing:
primitive → wrapper
Unboxing:
wrapper → primitiveFor example:
int number = 10;
Integer wrapper = number; // Autoboxing
int value = wrapper; // UnboxingUnderstanding wrapper classes is important because they connect Java's primitive types with its object-oriented type system and are used extensively throughout the Java Collections Framework, Generics, APIs, and application development.
See you in next page....🙂