JavaWrapper Classes

Wrapper Classes

Every one of Java's eight primitive types has a matching class that "wraps" that primitive value inside a full object. These are the wrapper classes, and they exist to bridge the gap between primitives — which are not objects — and the parts of Java that require objects.

The 8 Primitive-to-Wrapper Pairs

Primitive

Wrapper Class

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean

Note
Notice that most wrapper class names are just the capitalized primitive name (byteByte, longLong). The two exceptions to watch for are intInteger and charCharacter.
Why Wrapper Classes Exist

Primitives are extremely efficient, but they are not objects — and a significant part of Java, especially the generics system and the collections framework, is designed to work only with objects. Wrapper classes give primitive values an object identity so they can participate in that part of the language.

Reason 1: Generic Collections Require Objects
Generic types like ArrayList are defined to hold objects, not raw primitive values. You cannot parameterize a generic type with a primitive — you must use its wrapper class instead.

This does NOT compile

Java
import java.util.List;
import java.util.ArrayList;

public class BadGenericsDemo {
    public static void main(String[] args) {
        List<int> numbers = new ArrayList<int>(); // compile error!
    }
}
Warning
Compile-time error: unexpected type, found: int, required: class or interface. Java generics only work with reference types, and int is a primitive, not a reference type. You must use the wrapper class Integer instead.

The correct version

Java
import java.util.List;
import java.util.ArrayList;

public class GoodGenericsDemo {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        System.out.println(numbers);
    }
}
[10, 20, 30]
Note
You're able to write numbers.add(10) with a plain int literal thanks to autoboxing, which automatically converts it to an Integer behind the scenes. The dedicated Autoboxing & Unboxing page covers this conversion — and its gotchas — in depth.
Reason 2: Representing "No Value"
A primitive always has some concrete value — an int can never be "absent," only 0 or some other number. Wrapper classes, being reference types, can hold null, which is genuinely useful when you need to represent "this value hasn't been provided" or "unknown," as opposed to a specific numeric value like zero.

null represents an unknown score

Java
public class NullableScoreDemo {
    public static void main(String[] args) {
        Integer score = null; // "no score recorded yet"

        if (score == null) {
            System.out.println("No score recorded");
        } else {
            System.out.println("Score: " + score);
        }
    }
}
No score recorded
Tip
This is exactly why database results, optional configuration settings, and JSON fields that might be absent are almost always modeled with wrapper types (Integer, Boolean) rather than primitives — the wrapper type can represent "missing" and the primitive cannot.
Useful Utility Methods
Wrapper classes are not just containers — they come packed with static utility methods for converting and inspecting values. The most frequently used is parsing a primitive value out of a String.

Common wrapper utility methods

Java
public class WrapperUtilityDemo {
    public static void main(String[] args) {
        int fromString = Integer.parseInt("42");
        double priceFromString = Double.parseDouble("19.99");

        System.out.println(fromString + 8);
        System.out.println(priceFromString + 0.01);

        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);

        System.out.println(Integer.toBinaryString(42));
        System.out.println(Boolean.parseBoolean("true"));
    }
}
50
20.0
2147483647
-2147483648
101010
true

Method

Purpose

Integer.parseInt(str)

Convert a String to an int

Double.parseDouble(str)

Convert a String to a double

Integer.MAX_VALUE / MIN_VALUE

The largest/smallest possible int

Integer.toBinaryString(n)

Represent an int in binary

Boolean.parseBoolean(str)

Convert a String to a boolean

Integer.valueOf(n)

Get the Integer wrapper for an int

Tip
Reading user input or text from a file always gives you Strings. Wrapper class parsing methods like Integer.parseInt() are the standard way to turn that text back into usable numeric primitives.