Lesson 4.7: Wrapper Classes

AP CSA Hub Unit 4 4.1 Lesson Ex 1 Ex 2 Quiz 4.2 Lesson Ex 1 Ex 2 Quiz 4.3 Lesson Ex 1 Ex 2 Quiz 4.4 Lesson Ex 1 Ex 2 Quiz 4.5 Lesson Ex 1 Ex 2 Quiz 4.6 Lesson Ex 1 Ex 2 Quiz 4.7 Lesson Ex 1 Ex 2 Quiz 4.8 Lesson Ex 1 Ex 2 Quiz 4.9 Lesson Ex 1 Ex 2 Quiz 4.10 Lesson Ex 1 Ex 2 Quiz 4.11 Lesson Ex 1 Ex 2 Quiz 4.12 Lesson Ex 1 Ex 2 Quiz 4.13 Lesson Ex 1 Ex 2 Quiz 4.14 Lesson Ex 1 Ex 2 Quiz 4.15 Lesson Ex 1 Ex 2 Quiz 4.16 Lesson Ex 1 Ex 2 Quiz 4.17 Lesson Ex 1 Ex 2 Quiz

Unit 4 · Lesson 4.7 · Wrapper Classes

Lesson 4.7: Wrapper Classes

🕒 35-45 min · 10 Practice Questions · Autoboxing · parseInt · compareTo

What You'll Learn

  • Convert between primitive types and their wrapper classes (Integer, Double) using autoboxing and unboxing.
  • Use Integer.parseInt and Double.parseDouble to convert a String into a number.
  • Compare wrapped values correctly with compareTo, and explain why == is unreliable on objects.
  • Use the MIN_VALUE and MAX_VALUE constants 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

MCQ 1
Why can't a primitive int be stored directly in an ArrayList of int?
A int values are too large for a list to hold
B ArrayList can only hold objects, and int is a primitive, not an object; Integer must be used instead
C Java forbids lists of numbers entirely
D ArrayList<int> is valid Java and this is not actually a problem
B. Generic collections are typed to hold objects. Integer is the object form of int that makes ArrayList<Integer> possible.
MCQ 2
What is autoboxing?
A Manually calling Integer.valueOf every time a value is needed
B The compiler automatically converting a primitive value into its wrapper object when an object is required
C Converting a wrapper object into a String
D A compile error caused by mixing int and Integer
B. Autoboxing is the compiler-inserted conversion, not something the programmer writes out by hand.
MCQ 3
Which correctly converts the String "17" into a usable int?
A int x = "17";
B int x = Integer.parseInt("17");
C int x = (int) "17";
D int x = Integer("17");
B. A String cannot be assigned directly to an int, and it cannot be cast to one either. parseInt is the actual conversion.
MCQ 4
What does Integer.parseInt("abc") do?
A Returns 0
B Returns null
C Throws NumberFormatException at runtime
D Fails to compile
C. parseInt cannot make sense of non-numeric text, and it throws rather than guessing.
MCQ 5
Which correctly and reliably compares two Integer objects a and b for equal VALUE?
A a == b
B a.compareTo(b) == 0
C a.toString() == b.toString()
D a > b
B. == compares object identity, not value, on two Integer objects. compareTo compares the actual wrapped values.
Tier 3 · AP Mastery

Mastery: Wrapper Classes

MCQ 6
What is the risk in relying on this code always printing true?
Integer a = Integer.valueOf(500);
Integer b = Integer.valueOf(500);
System.out.println(a == b);
A None; Java always reuses Integer objects for any value
B It may print false: a and b can be different objects holding an equal value, and == compares object identity rather than value, which is exactly why == on wrapper objects is unreliable
C This code does not compile
D It always prints true for any two equal Integer values, without exception
B. Relying on == here depends on JVM-internal object caching behavior that is not guaranteed for every value, which is precisely the trap.
MCQ 7
What does this correctly detect, and why does the cast to long matter?
int sum = 2000000000;
System.out.println((long) sum * 2 > Integer.MAX_VALUE);
A It detects that doubling sum would overflow a 32 bit int; casting to long before multiplying avoids the overflow that would otherwise corrupt the comparison
B The cast does nothing; int arithmetic never overflows in Java
C This always prints false regardless of the value of sum
D Casting an int to long causes a compile error
A. 2,000,000,000 doubled is 4,000,000,000, which exceeds Integer.MAX_VALUE (2,147,483,647). Widening to long before multiplying is what lets the comparison see the true, un-overflowed value.
MCQ 8
Which converts a String like "3.14" into a usable decimal number?
A double x = Double.parseDouble("3.14");
B int x = Integer.parseInt("3.14");
C double x = "3.14";
D double x = Double.valueOf(3.14);
A. Integer.parseInt throws on a decimal point. A String cannot be assigned to a double directly. Double.valueOf(3.14) boxes an already-numeric literal rather than parsing text, which is not what converting a String requires.
MCQ 9
What is Integer.MAX_VALUE and why does it matter when adding two large int values?
A The largest value an int can hold (2147483647); adding two values whose sum exceeds it silently overflows, wrapping to a negative number rather than throwing
B A runtime limit that automatically throws an exception when exceeded
C A constant that only applies to Integer objects, never to a primitive int
D The same value as Long.MAX_VALUE, 9223372036854775807
A. int overflow in Java is silent, not an exception. That silence is exactly why an overflow check has to be written deliberately, as in the earlier example.
MCQ 10
A program keeps scores in ArrayList<Integer> scores. Which line correctly adds the primitive value 88?
A scores.add(88);
B scores.add(Integer(88));
C scores.add("88");
D scores.addInt(88);
A. Autoboxing converts 88 into Integer.valueOf(88) automatically at the call site. Integer(88) without new is not valid Java, "88" is a String not a number, and addInt is not a real ArrayList method.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]