AP Computer Science A · Unit 1: Using Objects & Methods
1.12 Wrapper Classes
What are wrapper classes?
Wrapper classes let primitive values be treated as objects. Each primitive type has a corresponding wrapper: `int` → Integer, `double` → Double, `boolean` → Boolean. Wrapper objects are needed when a program requires an object (e.g., storing values in generic collections like `ArrayList<Integer>`), since primitives alone cannot be used there.
Autoboxing and unboxing
Autoboxing is Java's automatic conversion of a primitive into its wrapper object (e.g., `int` → `Integer`) when needed. Unboxing is the reverse — converting a wrapper object back into a primitive. This happens automatically in assignments, method calls, and arithmetic, so code often looks the same whether primitives or wrappers are used.
Common wrapper methods & constants
Wrapper classes include useful static constants and methods, such as `Integer.MAX_VALUE` and `Integer.MIN_VALUE`, which give the largest and smallest values an `int` can hold. These are frequently tested for understanding integer overflow and boundary behavior.
Key terms
- Wrapper class
- A class that allows a primitive value to be treated as an object (e.g., Integer for int, Double for double).
- Autoboxing
- Automatic conversion of a primitive value into its corresponding wrapper object.
- Unboxing
- Automatic conversion of a wrapper object back into its primitive value.
- Integer.MAX_VALUE
- A constant representing the largest value an int can hold (2147483647).
- Why use wrapper classes?
- They let primitives be used where objects are required, such as in generic collections like ArrayList<Integer>.
Try an AP-style question
Why must ArrayList<Integer> be used instead of ArrayList<int>?
- ArrayList can only store Strings
- Generics need object types, not primitives
- Integer is always faster than int
- int is too small to store in a list
Show the answer
B. Wrapper classes.
Practice 8 AP-style questions with instant explanations, flashcards and spaced review.
Study 1.12 free in APrep