Lesson 4.7: Wrapper Classes
Lesson 4.7: Wrapper Classes
What You'll Learn
- Convert between primitive types and their wrapper classes (
Integer,Double) using autoboxing and unboxing. - Use
Integer.parseIntandDouble.parseDoubleto convert aStringinto a number. - Compare wrapped values correctly with
compareTo, and explain why==is unreliable on objects. - Use the
MIN_VALUEandMAX_VALUEconstants to reason about a type's range.
Key Vocabulary
| Term | Definition |
|---|---|
| wrapper class | A class like Integer or Double that holds a primitive value as an object, so it can be used wherever an object is required, such as inside an ArrayList. |
| autoboxing | The compiler automatically converting a primitive into its wrapper object, for example assigning an int where an Integer is expected. |
| unboxing | The reverse: automatically converting a wrapper object back into its primitive value to use in arithmetic. |
| Integer.parseInt | A static method that converts a String of digits into an int. Throws NumberFormatException on invalid input. |
| Integer.MAX_VALUE / MIN_VALUE | Named constants for the largest and smallest values an int can hold: 2147483647 and -2147483648. |
Why Wrapper Classes Exist
A generic collection like ArrayList<Integer> can only hold objects, and a primitive int is not an object. Integer is the object form of int that makes it possible to put whole numbers in a list at all.
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(95); // autoboxing: int -> Integer
int first = scores.get(0); // unboxing: Integer -> int
The compiler inserts the boxing and unboxing automatically. Nothing about the syntax looks different from working with a plain int, which is exactly the point: it should feel invisible until it is not.
Parsing Strings into Numbers
Text typed by a user, or read from a file, always arrives as a String, even when it looks like a number. Converting it into something you can do arithmetic on is a separate step.
String text = "42";
int value = Integer.parseInt(text);
String decimal = "3.14";
double d = Double.parseDouble(decimal);
⚠️ A Bad Token Throws, Not Fails Quietly
Integer.parseInt("abc") throws NumberFormatException at runtime. There is no silent failure mode: either the text parses or the program crashes right there, which is exactly why the shape of the input matters.
Comparing Wrapped Values
compareTo compares the VALUES two wrapper objects hold. == on two objects compares whether they are the same object in memory, which is a different question entirely.
Integer a = Integer.valueOf(200);
Integer b = Integer.valueOf(200);
System.out.println(a.compareTo(b)); // 0, meaning equal
⚠️ == on Wrapper Objects Is Not Reliable
Two Integer objects can hold the same value while being different objects, so == can return false even when the values are equal. compareTo (or equals) is the comparison that actually answers "do these hold the same value."
The Range Constants
int arithmetic that exceeds Integer.MAX_VALUE wraps around silently rather than throwing, which is why a check has to happen before the overflow, not after.
✅ Checking for Overflow Before It Happens
Widening to long before the multiplication lets the comparison see the true value instead of one that has already wrapped.
long doubled = (long) sum * 2; System.out.println(doubled > Integer.MAX_VALUE);
Practice Questions
Integer.parseInt("abc") do?
Mastery: Wrapper Classes
Integer a = Integer.valueOf(500); Integer b = Integer.valueOf(500); System.out.println(a == b);
int sum = 2000000000; System.out.println((long) sum * 2 > Integer.MAX_VALUE);
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]