AP CSA Unit 1 Day 13: III Wrapper Behavior

Unit 1 Advanced (Cycle 2) Day 13 of 28 Advanced

I/II/III: Wrapper Class Behavior

Section 1.12 — Wrapper Classes

Key Concept

I/II/III questions about wrapper classes test autoboxing, unboxing, and equality semantics. Key facts: Integer a = 5 autoboxes the primitive, int b = a unboxes it, and a == b compares by value because b triggers unboxing of a. However, comparing two Integer objects with == checks reference equality. Java caches Integer values from -128 to 127, so == may return true for small values but false for larger ones. The exam tests whether you understand when autoboxing occurs and when it does not.

Consider the following statements about wrapper classes.

I. Integer x = 5; int y = x; This compiles successfully. II. Integer a = new Integer(10); Integer b = new Integer(10); (a == b) evaluates to true. III. Double d = 7.5; double p = d; This compiles successfully.

Which of the statements are correct?

Answer: (C) I and III only

Evaluate each statement:

I: CORRECT. Integer x = 5 autoboxes int to Integer. int y = x unboxes Integer to int. Both compile.

II: INCORRECT. new Integer(10) creates two separate objects. == compares references, not values. Since they are different objects, a == b is false.

III: CORRECT. Double d = 7.5 autoboxes double to Double. double p = d unboxes Double to double. Both compile.

Why Not the Others?

(A) Statement III is also correct. Autoboxing and unboxing work for Double/double just as they do for Integer/int.

(B) Statement II is incorrect. Two objects created with new are always different references. == checks references, so a == b is false.

(D) Statement II is the trap. The == operator compares object references, not the values inside wrapper objects.

Common Mistake

The == trap with wrapper classes is one of the most tested AP exam concepts. Even if two Integer objects hold the same value, == returns false when they are different objects. Always use .equals() for value comparison.

AP Exam Tip

For I/II/III questions, the most commonly incorrect statement involves == with objects. Whenever you see == comparing reference types (Integer, Double, String created with new), it almost certainly evaluates to false.

Review this topic: Section 1.12 — Wrapper Classes • Unit 1 Study Guide

More Practice

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.