Topic 4.7 Notes – Wrapper Classes
1. What Wrapper Classes Are
A wrapper class is the object version of a primitive type.
int→ Integerdouble→ Double
They’re in the java.lang package, which means you don’t import them.
Primitive vs Wrapper
- A primitive stores a raw value directly.
- A wrapper stores a value inside an object, and the variable holds a reference to that object.
That difference explains almost every wrapper bug you’ll see.
Immutability
Both Integer and Double are immutable.
Integer x = 10;
x = x + 5;
You didn’t change the old object. You created a new Integer and reassigned x.
2. Autoboxing and Unboxing
Java quietly converts between primitives and wrappers for you. This is where most test questions live.
a. Autoboxing (primitive → wrapper)
Autoboxing happens when Java turns a primitive into its wrapper automatically.
Integer a = 5; // int → Integer
Double d = 3.2; // double → Double
This also happens when:
- Passing a primitive to a method that expects a wrapper
- Adding to an
ArrayList<Integer>
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(8); // int autoboxed to Integer
Behind the scenes, Java is doing something like:
Integer.valueOf(5);
You don’t write it. The compiler inserts it.
b. Unboxing (wrapper → primitive)
Unboxing is the reverse.
Integer x = 12;
int y = x; // Integer → int
Also happens in arithmetic:
Integer num = 4;
int total = num + 6; // num unboxed first
Java automatically extracts the primitive value.
The Null Danger
This is the most common runtime crash with wrappers.
Integer value = null;
int n = value; // NullPointerException
Java tries to unbox null. There is no value inside. Program crashes.
If you ever see arithmetic with Integer variables, mentally ask yourself, “Could this be null?”
This shows up a lot with ArrayList<Integer> because lists can store null.
3. Integer and Double Methods You Must Know
These are on the Java Quick Reference.
a. Integer.parseInt(String s)
int num = Integer.parseInt("42");
- Takes a
String - Returns a primitive
int - Used when converting text to numbers
Common situation: processing user input or splitting a string.
Important detail: it returns int, not Integer.
b. Double.parseDouble(String s)
double val = Double.parseDouble("3.14");
- Takes a
String - Returns a primitive
double
Same idea as parseInt, just for decimals.
Wrapper vs Primitive Return
Keep this straight:
parseInt→intparseDouble→double
They do not return wrapper objects.
4. Comparing Integer Objects Correctly
This is a classic AP multiple-choice trap.
== vs equals()
| Comparison | What it checks | Correct for Integer? |
|---|---|---|
== | Same object (same reference) | No |
equals() | Same stored value | Yes |
Example:
Integer a = 150;
Integer b = 150;
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
Why?
==checks if both variables point to the same object.equals()checks if the values inside are equal.
Rule you should hear in your head during tests:
- Use
==for primitives. - Use
equals()for wrapper objects.
If you mix this up on an MCQ, the wrong answer will look very tempting.
5. How Wrapper Classes Show Up on Tests
ArrayList<Integer> Patterns
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3); // autoboxing
int x = nums.get(0); // unboxing
Watch for:
nullvalues in the list- Arithmetic with
Integer - Comparisons using
==instead ofequals()
Code Tracing Strategy
When tracing:
- Identify every autoboxing.
- Identify every unboxing.
- Check for possible
null. - Check whether comparisons use
==orequals().
A lot of wrapper questions aren’t hard. They’re subtle.