UNIT 4: DATA COLLECTIONS · TOPIC 4.7
4.7 Wrapper Classes
ArrayLists can't hold primitives, so Java wraps them in objects. Autoboxing makes this mostly invisible — except in the spots the exam tests.
What you need to know
IntegerandDoubleare wrapper classes: objects that hold one int or double. Needed becauseArrayList<int>is illegal; you writeArrayList<Integer>.- Autoboxing: Java automatically converts int → Integer when needed (
list.add(5)). Unboxing: Integer → int (int x = list.get(0)). Integer.MAX_VALUEandInteger.MIN_VALUE— the int range limits, on the reference sheet. Useful as initial values for min/max algorithms.Integer.parseInt(String)andDouble.parseDouble(String)convert text to numbers — essential for file input.- Comparing two Integer objects with
==compares references and is unreliable; use.equals()or unbox to ints first. - Unboxing a
nullInteger throws a NullPointerException. - Arithmetic on Integers unboxes automatically:
Integer a = 5; int b = a + 3;works.
Worked example
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(7); // autobox int → Integer
int first = nums.get(0); // unbox Integer → int
Integer big = Integer.MAX_VALUE;
int n = Integer.parseInt("42"); // 42
double d = Double.parseDouble("3.5");
int min = Integer.MAX_VALUE; // start high for a min search
for (int x : nums)
{
if (x < min) min = x;
}
Exam tip: You can't write
ArrayList<int> — that's a guaranteed compile-error question. Everywhere else, treat Integer like int and let autoboxing work, except: never compare Integers with ==, and remember a null Integer crashes when unboxed.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Why wrappers exist: generics (
ArrayList<T>) only work with reference types.intis a primitive.Integeris a class whose objects hold one int. SoArrayList<Integer>is the way to store ints. - Autoboxing (int → Integer) happens automatically on
add, on assignment to an Integer variable, on passing an int where an Integer is expected. Unboxing (Integer → int) happens on assignment to an int, in arithmetic, in comparisons with< >. You rarely write the conversion. - The == trap:
Integer a = 1000, b = 1000; a == bis false (different objects). For small values (−128 to 127) Java caches, so it's true — which makes the bug intermittent. Use.equalsor unbox to int. The exam avoids relying on caching. - Null unboxing:
Integer x = null; int y = x;throws NullPointerException. An ArrayList slot that was never set is null; unboxing it crashes. Integer.MAX_VALUE/MIN_VALUE: the int range. Standard initial values for min/max searches — guaranteed to be replaced by any real value.Integer.parseInt("42")→ 42.Double.parseDouble("3.5")→ 3.5. Non-numeric input throwsNumberFormatException. These are how file and keyboard text become numbers.- Arithmetic on Integers auto-unboxes:
Integer a = 5; a + 3is 8 (an int).list.get(0) + list.get(1)adds two unboxed ints.
Mistakes that cost points
- ArrayList<int>. Compile error. Guaranteed question.
- == on Integer objects. Unreliable. Unbox or use equals.
- Unboxing null. NPE.
- Starting a min search at 0. Use Integer.MAX_VALUE or the first element.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following declarations compiles?
Show answer
Answer: B. Generic type parameters must be classes; Integer is the wrapper for int.
Q2 A method finds the largest value in a list of integers by starting with
int max = 0;. For which input does this produce a wrong answer, and what initial value fixes it?Show answer
Answer: B. If every value is negative, none exceed 0. Starting at Integer.MIN_VALUE (or the first element) fixes it.
Key vocabulary
- Wrapper class
- an object type (Integer, Double) that holds a primitive value
- Autoboxing
- automatic conversion from a primitive to its wrapper
- Unboxing
- automatic conversion from a wrapper to its primitive
- Integer.parseInt
- converts a String to an int