Sub topic 2
Meet the Wrapper Classes
Java has two kinds of values: primitive values and references to objects. This chapter assumes you know Java primitive types such as int, long, double, boolean, and char, and that you have basic experience with classes, objects, ==, equals, and generic collections such as List<Integer>.
A wrapper class is an object type that represents one primitive value. For example, Integer wraps an int, and Double wraps a double.
The eight wrapper classes
Java provides one main wrapper class for each primitive type:
| Primitive type | Wrapper class | Example |
|---|---|---|
| `byte` | `Byte` | `Byte.valueOf((byte) 8)` |
| `short` | `Short` | `Short.valueOf((short) 16)` |
| `int` | `Integer` | `Integer.valueOf(42)` |
| `long` | `Long` | `Long.valueOf(42L)` |
| `float` | `Float` | `Float.valueOf(3.5f)` |
| `double` | `Double` | `Double.valueOf(3.5)` |
| `char` | `Character` | `Character.valueOf('A')` |
| `boolean` | `Boolean` | `Boolean.valueOf(true)` |
The numeric wrappers from Byte through Double extend the abstract Number class. Character and Boolean do not extend Number.
The Void class is also part of the wrapper-class family in the Java API. It represents the void keyword, but it does not wrap a value because void has no value.
Why wrapper objects exist
Primitive values are efficient, but they are not objects. Some Java features require objects. Generic type arguments are the most common example.
You cannot write List<int>, because a generic type parameter must be a reference type. You can write List<Integer> instead:
import java.util.ArrayList;
import java.util.List;
public class WrapperExample {
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>();
scores.add(Integer.valueOf(90));
scores.add(Integer.valueOf(85));
Integer firstScore = scores.get(0);
System.out.println(firstScore);
}
}The list stores references to Integer objects. The primitive type int cannot be used directly as the element type.
Wrappers also provide useful methods and constants:
Integer.MAX_VALUEgives the largest value of typeint.Long.compare(a, b)compares twolongvalues.Double.isNaN(value)checks for “not a number”.Character.isDigit(ch)checks whether a character is a digit.Boolean.parseBoolean(text)converts text to a boolean value.
A wrapper can also represent the absence of a value with null. A primitive cannot be null.
The connection between primitives and wrappers
A primitive and its wrapper are different types:
int primitiveCount = 12;
Integer objectCount = Integer.valueOf(12);
System.out.println(primitiveCount);
System.out.println(objectCount);The Integer object contains an int value, but it also has object identity and methods. This difference matters when values are stored in collections, passed to overloaded methods, compared, or converted.
Java can often move between the two forms automatically. Assigning an int to an Integer is called boxing. Extracting an int from an Integer is called unboxing. These conversions are covered in detail in the next chapter.
Choosing a wrapper deliberately
A wrapper is not automatically a better version of a primitive. It has additional behavior, but it may require an object reference and may be null.
For example, Integer can distinguish these two states:
Integercontaining0: a real numeric value.null: no numeric value is available.
That distinction is useful for optional database columns or partially completed input. It is unnecessary when every calculation always has a valid number. In that case, int communicates the rule more clearly.
Exercise
Which type should you choose for a list of whole-number scores, and why?
Show answer
Use List<Integer> because Java generics require a reference type. The list cannot use List<int>.