Wrapper Classes in Java

Naveen AnamaneniUpdated · 4 min read

Primitive values like int and double are fast and simple, but they aren't objects. They can't be stored in an ArrayList, can't be null, and have no methods. For each of the eight primitives, Java provides a wrapper class, an object that holds one primitive value: Integer for int, Double for double, and so on. Wrappers also carry useful constants and helper methods, like Integer.MAX_VALUE and Character.isDigit.

Eight Primitives, Eight Wrappers

A wrapper is an object with a primitive inside
PrimitiveWrapperExample
byteByteByte.MAX_VALUE is 127
shortShort
intIntegerNot Int
longLong
floatFloat
doubleDouble
charCharacterNot Char
booleanBoolean

All wrappers live in java.lang, are immutable and final. The six numeric wrappers extend java.lang.Number.

Why Wrappers Exist

Wrap.java▶ Try it
import java.util.ArrayList;
import java.util.List;
 
public class Wrap {
    public static void main(String[] args) {
        List<Integer> marks = new ArrayList<>();
        marks.add(91);
        marks.add(78);
        System.out.println("Marks: " + marks);
 
        Integer pending = null;
        System.out.println("Pending result: " + pending);
 
        System.out.println(Integer.MAX_VALUE + " " + Long.MAX_VALUE);
        System.out.println(Integer.toBinaryString(10)
            + " " + Integer.toHexString(255));
        System.out.println(Character.isDigit('7')
            + " " + Character.toUpperCase('n'));
        System.out.println(Integer.compare(3, 7) + " " + Integer.max(3, 7));
 
        Object any = 3.14;
        System.out.println(any.getClass().getSimpleName());
    }
}
Output
Marks: [91, 78]
Pending result: null
2147483647 9223372036854775807
1010 ff
true N
-1 7
Double

How it works

  1. Collections need objects. List<int> isn't allowed; List<Integer> is. Java converts 91 to an Integer automatically (autoboxing, next lesson).
  2. Wrappers can be null, which can mean "no value yet", such as a result that hasn't been published.
  3. Constants: MIN_VALUE and MAX_VALUE give each type's range.
  4. Helper methods: converting to binary or hex, checking characters, comparing values.
  5. A primitive stored in an Object variable becomes its wrapper: 3.14 became a Double.

Primitive or Wrapper?

Use a primitive (int) whenUse a wrapper (Integer) when
Doing arithmetic, loops, countersStoring in collections: List<Integer>, Map<String, Double>
Performance mattersA value may be absent (null)
The value always existsUsing generics: Optional<Integer>
Fields and local variables in generalWorking with APIs that need objects

Common Mistakes

Writing Int or Char. The wrappers are Integer and Character.

Comparing Integer objects with ==. Use equals, or unbox to int first.

Using wrappers for every field. They cost more memory and can be null; use primitives unless you need an object.

Interview Corner

What are wrapper classes and why are they needed?

Wrapper classes are object versions of the eight primitives: Byte, Short, Integer, Long, Float, Double, Character and Boolean. They're needed because collections and generics only work with objects, because they can represent the absence of a value with null, and because they provide constants and utility methods such as Integer.parseInt and Character.isDigit. They're immutable and final.

Practice

  1. Easy: Print MIN_VALUE and MAX_VALUE for Byte, Short, Integer and Long.
  2. Harder: Count the digits, letters and spaces in "Navika Anamaneni 2026" using Character methods.

Quick check: Wrapper classes

4 questions
1What is the wrapper class for char?
2Why can't you write List<int>?
3Which can hold null?
4Are wrapper objects mutable?

Key Takeaways

Was this tutorial helpful?

Lesson 159 of 482