CodeGym /Courses /JAVA 25 SELF /Wrapper types in Java

Wrapper types in Java

JAVA 25 SELF
Level 10 , Lesson 1
Available

1. Introduction

Java has two different families of data. The first is primitive types: int, char, double, boolean, and others. They are simple and fast: the value is stored directly in a memory cell. The second family is reference types, i.e., objects: a variable stores a reference to the place where the object resides in memory.

Sometimes you need to use a primitive as an object. But primitives are not objects by design. In such cases, special wrapper classes help.

Wrapper types are classes that store the value of a primitive inside and allow you to treat it as an object. For example, for int — the Integer class, for doubleDouble, for charCharacter, for booleanBoolean. Such classes provide methods: Integer.parseInt(), Double.isInfinite(), Character.isLetter(), Boolean.parseBoolean(), etc.

Example of getting an Integer object from an int primitive:

// Primitive
int a = 10;

// Wrapper object
Integer b = Integer.valueOf(10);

The variable a stores the number 10 itself. And the variable b stores a reference to an object that contains 10 inside.

2. Why wrappers are needed

A primitive is always a concrete value; you cannot set it to null. An object may be absent and hold an empty value—this is convenient when you need to denote “unknown” or “missing”.

Primitives have no methods. You can’t call something like x.toString() on a number. A wrapper provides such capabilities: Integer.parseInt("123") turns a string into a number, and you can turn a number into a string using toString().

In short: wrappers are needed where an object is required—the ability to hold null, call methods, and pass values to an API that accepts only objects (for example, collections like List<Integer>).

Full list of wrapper types

Primitive type Wrapper class
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean

3. Boxing and unboxing

When you manually create a wrapper object, you take a primitive and “box” it inside the object:

int primitive = 42;
Integer wrapper = Integer.valueOf(primitive); // boxing

To retrieve the primitive value back—“unbox” it—you use, for example, intValue():

Integer wrapper = Integer.valueOf(42);
int primitive = wrapper.intValue(); // unboxing

Autoboxing

The compiler will add a call to valueOf() for you when you assign a primitive to a wrapper variable:

int a = 10;
Integer b = a; // autoboxing (Integer.valueOf(a))

Unboxing

The reverse situation is when a wrapper object is used where a primitive is needed:

Integer c = 20;
int d = c; // unboxing (c.intValue())

int a = 10; // primitive
Integer b = a; // autoboxing
Integer c = Integer.valueOf(20);
int d = c; // unboxing
Autoboxing and unboxing

4. Pitfalls

Comparing wrappers. The == operator for objects compares references, not values:

Integer m = 200;
Integer n = 200;
System.out.println(m == n); // false, because these are different objects

Java caches number wrappers in the range from -128 to 127, so sometimes == returns true and sometimes false. To compare values, always use equals():

Integer x = 100;
Integer y = 100;
System.out.println(x == y);      // true, hit the cache
System.out.println(x.equals(y)); // true, value comparison

NullPointerException during unboxing. If a wrapper is null and you try to unbox it, you’ll get an exception:

Integer value = null;
int primitive = value; // Error! NullPointerException

Performance and memory. Wrappers are slower than primitives and take more memory. In hot code paths and when working with large arrays of numbers, prefer primitives.

5. Examples

Examples with Integer and Double

Converting a string to a number:

String text = "123";
int number = Integer.parseInt(text);
System.out.println(number); // 123

Checking special floating-point values:

double d = 1.0 / 0;
System.out.println(Double.isInfinite(d)); // true

double nan = 0.0 / 0.0;
System.out.println(Double.isNaN(nan)); // true

Examples with Character

char stores a single character; the Character wrapper provides handy checks:

char ch = 'A';
Character wrapper = ch; // autoboxing

System.out.println(Character.isLetter(ch)); // true
System.out.println(Character.isDigit(ch));  // false
System.out.println(Character.toLowerCase(ch)); // 'a'

Examples with Boolean

The boolean primitive is only true/false. The Boolean wrapper also allows storing null:

Boolean flag = null; // allowed
flag = Boolean.TRUE; // special constant

System.out.println(flag); // true

Converting strings to boolean values:

String s1 = "true";
String s2 = "false";
boolean b1 = Boolean.parseBoolean(s1);
boolean b2 = Boolean.parseBoolean(s2);

System.out.println(b1); // true
System.out.println(b2); // false
1
Task
JAVA 25 SELF, level 10, lesson 1
Locked
Player Score Recording 🎮
Player Score Recording 🎮
1
Task
JAVA 25 SELF, level 10, lesson 1
Locked
Digit Input Validation 🔢
Digit Input Validation 🔢
1
Task
JAVA 25 SELF, level 10, lesson 1
Locked
Transferring Price Tags to a Digital Cart 🛒
Transferring Price Tags to a Digital Cart 🛒
1
Task
JAVA 25 SELF, level 10, lesson 1
Locked
Handling "broken" data 📉
Handling "broken" data 📉
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 38, San Diego, United States
4 April 2026
Primitives have no methods