So far, we've worked with primitive data types such as int, double, char, and boolean.
For example:
int age = 20;
double price = 99.99;
char grade = 'A';
boolean isOnline = true;These are primitive types. They are simple and useful, but sometimes Java needs us to work with these values as objects.
This is where wrapper classes come in.
A wrapper class is a class that provides an object representation of a primitive data type.
For example:
int → Integer
double → Double
char → Character
boolean → BooleanSo you can think of a wrapper class as a way to wrap a primitive value inside an object.
Primitive Types and Wrapper Classes
Java has eight primitive data types, and each has a corresponding wrapper class:
Primitive | Wrapper Class |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example:
int age = 20;
Integer number = 20;Here, age is a primitive int, while number is an Integer object.
Why Do We Need Wrapper Classes?
One common reason is that collections work with objects, not primitive types.
For example, this is not allowed:
ArrayList<int> numbers = new ArrayList<>(); // ErrorInstead, we use the wrapper class:
ArrayList<Integer> numbers = new ArrayList<>();Now we can add integers:
numbers.add(10);
numbers.add(20);
numbers.add(30);This is why you've already seen Integer when working with ArrayList.
The same idea applies to other primitive types:
ArrayList<Double> prices = new ArrayList<>();
ArrayList<Character> letters = new ArrayList<>();
ArrayList<Boolean> results = new ArrayList<>();Creating Wrapper Objects
We can create wrapper objects using their constructors, although this is generally not the preferred approach in modern Java.
For example:
Integer number = Integer.valueOf(100);
Double price = Double.valueOf(99.99);
Boolean result = Boolean.valueOf(true);Java also allows us to simply write:
Integer number = 100;This is much more common and convenient.
Autoboxing
Java provides a feature called autoboxing.
Autoboxing means Java automatically converts a primitive value into its corresponding wrapper object when needed.
For example:
int number = 10;
Integer value = number;Java automatically converts the int into an Integer.
You don't have to manually create the wrapper object.
Another example:
ArrayList<Integer> numbers = new ArrayList<>();
int number = 50;
numbers.add(number);numbers expects an Integer, but we provide an int.
Java automatically performs the conversion.
So:
int → Integerautomatically when required.
Unboxing
The opposite process is called unboxing.
Unboxing means Java automatically converts a wrapper object back into its primitive type.
For example:
Integer number = 100;
int value = number;Java automatically converts:
Integer → intWe can see both processes together:
int number = 10;
Integer wrapped = number; // Autoboxing
int unwrapped = wrapped; // UnboxingJava handles these conversions automatically.
Converting Strings to Wrapper Types
Wrapper classes also provide useful methods for converting Strings into numbers.
For example:
String text = "100";
int number = Integer.parseInt(text);
System.out.println(number);Output:
100Similarly, we can convert a String to a double:
String text = "99.99";
double price = Double.parseDouble(text);We can also use:
Long.parseLong("1000");
Float.parseFloat("10.5");
Boolean.parseBoolean("true");These methods are particularly useful when working with user input because input is often received as text.
Useful Wrapper Class Methods
Wrapper classes provide several useful methods.
For example, Integer has:
Integer.parseInt("100");which converts a String into an int.
It also has:
Integer.valueOf("100");which returns an Integer object.
We can also check the maximum and minimum values of an integer:
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);Output:
2147483647
-2147483648This can be useful when you need to understand the range of a primitive type.
Primitive vs Wrapper
The main difference is that a primitive is a basic data value, while a wrapper is an object representing that value.
For example:
int age = 20;
Integer ageObject = 20;int is generally simpler and more efficient when you just need to perform calculations.
Integer is useful when you need an object, such as when working with collections or APIs that require objects.
A Simple Example
Let's see wrapper classes in a practical example:
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
int firstNumber = numbers.get(0);
System.out.println(firstNumber);
}
}Output:
10Here, the ArrayList stores Integer objects, but when we retrieve the value:
int firstNumber = numbers.get(0);Java automatically performs unboxing from Integer to int.
The main idea to remember is:
Wrapper classes provide object versions of Java's primitive data types.
The most commonly used ones are Integer, Double, Character, and Boolean. They become especially important when working with collections, generics, and situations where Java requires an object instead of a primitive value.