Wrapper Classes in Java
In this lesson: Integer, Double, Character wrapper classes in Java — autoboxing, unboxing, parse methods — with output. In Hindi & English.
What are wrapper classes
Wrapper classes turn a primitive type into an object. Each primitive has a matching class: int → Integer, double → Double, and so on. This is needed because some parts of Java (like collections) work only with objects, not primitives.
The wrapper classes
| Primitive | Wrapper |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
| long | Long |
Each of the eight primitives has a wrapper class. Note that most just capitalise the name, but int becomes Integer and char becomes Character.
Autoboxing and unboxing
Integer boxed = 5; // autoboxing: int -> Integer int unboxed = boxed; // unboxing: Integer -> int System.out.println(boxed + " " + unboxed);
Java converts automatically: autoboxing wraps an int into an Integer, and unboxing takes the int back out. You usually do not convert by hand — Java handles it.
Why we need them
import java.util.ArrayList; ArrayList<Integer> list = new ArrayList<>(); list.add(10); // autoboxed to Integer list.add(20); System.out.println(list.get(0));
Collections store objects, so you write ArrayList<Integer>, not ArrayList<int>. Wrapper classes let numbers live in these object-based collections, with autoboxing doing the conversion.
Parsing strings to numbers
int n = Integer.parseInt("123");
double d = Double.parseDouble("9.5");
System.out.println(n + d);
Wrapper classes provide parseInt and parseDouble to turn text into numbers — essential when reading numeric input as strings.
Useful wrapper methods
System.out.println(Integer.MAX_VALUE); // biggest int
System.out.println(Integer.toBinaryString(10)); // 1010
System.out.println(Character.isDigit('5')); // true
1010
true
Wrapper classes carry handy constants and methods, like Integer.MAX_VALUE, base conversions, and character checks — things a plain primitive cannot offer.
Common mistakes
- Using a primitive in a collection. Use the wrapper:
ArrayList<Integer>, not<int>. - Null unboxing. Unboxing a null Integer to int throws a NullPointerException.
- Comparing wrappers with
==. Useequals()for value comparison of Integer objects. - Wrong wrapper name. It is
IntegerandCharacter, notIntorChar.
Summary
- Wrapper classes turn primitives into objects (int → Integer).
- Autoboxing and unboxing convert between them automatically.
- Collections need wrappers because they store objects.
- Use
parseInt/parseDoubleto convert strings to numbers. - Wrappers add useful constants and methods primitives lack.
Next, store them in an ArrayList, or convert types with type casting.
Frequently Asked Questions
What is a wrapper class in Java?
Integer wraps int, and Double wraps double. This lets primitives be used where objects are required, like in collections.What is autoboxing and unboxing?
Why do collections need wrapper classes?
ArrayList<Integer>, not ArrayList<int>. Wrapper classes let primitive values live inside these object-based collections.How do I convert a String to an int?
Integer.parseInt("123"), which returns the int 123. Similarly Double.parseDouble("9.5") gives a double. These wrapper methods are the standard way to turn text into numbers.What is the difference between int and Integer?
int is a primitive that stores a value directly and cannot be null. Integer is an object that wraps an int, can be null, and has useful methods, but uses a little more memory.