Java Wrapper Classes

Java has eight simple data types such as int and double. These types hold plain values and carry no extra abilities. A wrapper class puts a plain value inside an object. The object brings useful methods and works in places where plain values cannot go.

A Gift Box Example

Imagine a loose coin in your pocket. The coin has value but no label and no extras. Place the coin in a gift box with a label and a handy tool kit. The box now travels through the mail, fits on shelves, and shows what it holds. A wrapper class does the same job for a value.

The Eight Wrapper Classes

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Diagram: Primitive and Wrapper

Primitive                   Wrapper object
+------+                    +-----------------------+
|  25  |   --- boxing -->   | Integer               |
+------+                    |   value: 25           |
 plain value                |   methods: compareTo, |
                            |   toString, intValue  |
                            +-----------------------+

Autoboxing and Unboxing

Java wraps and unwraps values for you. Wrapping a primitive into an object is called autoboxing. Pulling the primitive back out is called unboxing.

int a = 10;
Integer box = a;      // autoboxing
int b = box;          // unboxing
System.out.println(box + b);   // 20

Why Wrapper Classes Matter

Collections Need Objects

Lists, sets, and maps store objects only. You cannot write ArrayList of int. Write ArrayList of Integer instead.

ArrayList<Integer> marks = new ArrayList<>();
marks.add(90);
marks.add(85);

Handy Utility Methods

Wrapper classes convert text to numbers and compare values.

int n = Integer.parseInt("42");
double d = Double.parseDouble("3.5");
String s = Integer.toString(99);
int big = Integer.MAX_VALUE;
int result = Integer.compare(5, 8);   // negative number

Support for Missing Values

A primitive always holds a value. A wrapper variable can hold null, which means no value yet. Databases and forms use this idea for empty fields.

Integer score = null;   // no score recorded

Comparing Wrapper Objects

Use the equals method to compare wrapper values. The double-equals sign compares object addresses and can mislead you. Java caches small Integer values from -128 to 127, so double-equals works for small numbers and fails for large ones.

Integer x = 127, y = 127;
Integer p = 1000, q = 1000;
System.out.println(x == y);        // true
System.out.println(p == q);        // false
System.out.println(p.equals(q));   // true

Common Mistakes

  • Unboxing a null wrapper, which throws a NullPointerException
  • Comparing large wrapper values with double-equals
  • Passing text like "abc" to parseInt, which throws a NumberFormatException
  • Using wrappers in heavy loops, which wastes memory

Wrapper Classes in Daily Programs

Reading Numbers from Users

Keyboard input arrives as text. A shopping app reads the text "250" and must turn it into a number before adding tax. The parseInt method does this job in one line.

Checking Characters

The Character class tests single letters. Login forms use these methods to check passwords.

char ch = '7';
System.out.println(Character.isDigit(ch));    // true
System.out.println(Character.isLetter(ch));   // false
System.out.println(Character.toUpperCase('a')); // A

Limits of Each Type

Each numeric wrapper stores the smallest and largest value its type can hold. Use MIN_VALUE and MAX_VALUE to read these limits.

Quick Summary

  • Each primitive type has a matching wrapper class
  • Autoboxing and unboxing convert values automatically
  • Collections store wrapper objects
  • Use equals to compare wrapper values