4.7 Wrapper Classes
💻AP Computer Science A Unit 4 Review
4.7 Wrapper Classes
💻 AP Computer Science A
4.1
Ethical and Social Implications
4.2
Introduction to Using Data Sets
4.3
Array Creation and Access
4.4
Traversing Arrays
4.5
Developing Algorithms Using Arrays
4.6
Using Text Files
4.7
Wrapper Classes: Integer and Double
4.8
ArrayList Methods
4.9
Traversing ArrayLists
4.10
Developing Algorithms Using ArrayLists
4.11
2D Arrays
4.12
Traversing 2D Arrays
4.13
Implementing 2D Array Algorithms
4.14
Searching
4.15
Sorting
4.16
Recursion
4.17
Recursive Searching and Sorting
Wrapper classes let you use Integer and Double objects in places that need objects instead of primitives, like an ArrayList. Java handles conversions automatically through autoboxing from primitive to wrapper and unboxing from wrapper to primitive, and you can turn a String into a number with Integer.parseInt or Double.parseDouble. For AP Computer Science A, track whether a value is a primitive, wrapper object, or parsed number.
You are responsible for two wrapper methods from the Java Quick Reference: Integer.parseInt(String s) and Double.parseDouble(String s). Wrapper classes also connect primitives to the object-based collections, like ArrayList, that you use later in Unit 4.
How Java Converts Between Primitives and Wrappers
IntegerandDoubleare wrapper classes in thejava.langpackage, so no import is needed, and both are immutable once created.- Autoboxing converts a primitive to its wrapper automatically; unboxing converts a wrapper back to a primitive automatically.
- Java autoboxes when a primitive is assigned to a wrapper variable or passed to a method expecting that wrapper.
- Java unboxes when a wrapper is assigned to a primitive variable or passed to a method expecting that primitive.
Integer.parseInt(String s)returns anint, andDouble.parseDouble(String s)returns adouble.- Wrapper objects are needed for collections like
ArrayList, which store object references rather than primitives.
Key Concepts
Integer and Double Are Wrapper Classes
A wrapper class gives a primitive value an object form. The Integer class wraps an int, and the Double class wraps a double. Both live in the java.lang package, which is available automatically, so you do not write an import statement to use them.
Both Integer and Double are immutable. Once an Integer or Double object is created, its value cannot be changed. If you need a different value, you create a new object rather than modifying the existing one.
The main reason wrapper classes exist for the exam is that some structures only hold objects, not primitives. An ArrayList stores object references, so you store numbers as Integer or Double objects rather than as raw int or double values.
Autoboxing: Primitive to Wrapper
Autoboxing is the automatic conversion the Java compiler makes from a primitive to its matching wrapper class. This includes turning an int into an Integer and a double into a Double. You do not write any special code; the compiler does it for you.
Autoboxing happens in two situations:
- A primitive value is assigned to a variable of the wrapper class.
- A primitive value is passed as a parameter to a method that expects the wrapper class.
</>JavaInteger count = 42; // int 42 is autoboxed into an Integer Double price = 3.99; // double 3.99 is autoboxed into a Double ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); // int 10 is autoboxed before being added
In the add call, the list expects an Integer, so the int literal 10 is autoboxed automatically.
Unboxing: Wrapper to Primitive
Unboxing is the reverse: the automatic conversion from a wrapper object back to its primitive type. This includes turning an Integer into an int and a Double into a double.
Unboxing happens in two situations:
- A wrapper object is assigned to a variable of the matching primitive type.
- A wrapper object is passed as a parameter to a method that expects the matching primitive.
</>JavaInteger boxed = 7; int value = boxed; // Integer is unboxed into an int ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(5); int first = nums.get(0); // get(0) returns an Integer, which is unboxed to int int total = 0; for (Integer n : nums) { total += n; // n is unboxed to int for the addition }
Because autoboxing and unboxing are automatic, you can often write code with plain numbers and let the compiler handle the conversions. The skill the exam tests is recognizing where those conversions happen so you can predict the result.
Converting Strings to Numbers
Two static methods convert text into numbers, and both are on the Java Quick Reference:
Integer.parseInt(String s)returns theStringargument as anint.Double.parseDouble(String s)returns theStringargument as adouble.
</>Javaint n = Integer.parseInt("123"); // n is 123 double d = Double.parseDouble("3.14"); // d is 3.14
These are useful when data comes in as text and you need an actual number to compute with. Notice that parseInt returns a primitive int and parseDouble returns a primitive double, not wrapper objects.
Handling Number Conversions When Tracing and Writing Code
Code Tracing: When you trace code that mixes primitives and wrappers, label each conversion as you go. If a primitive is assigned to a wrapper variable or passed where a wrapper is expected, that is autoboxing. If a wrapper is assigned to a primitive variable or passed where a primitive is expected, that is unboxing. Tracking these conversions keeps you from getting confused about which type a variable holds.
For ArrayList<Integer>, remember that add autoboxes the number going in and get returns an Integer that gets unboxed when you store it in an int or use it in arithmetic.
Free Response: Free-response code writing that processes a collection of numbers usually relies on autoboxing and unboxing. When you build an ArrayList<Integer>, you can add plain int values and they are boxed automatically. When you sum or compare elements, the wrappers are unboxed for the arithmetic. Write the natural code with numbers and trust the automatic conversions, but know that the list holds Integer objects.
Common Trap: If you need to turn a String into a number, reach for Integer.parseInt or Double.parseDouble. Mixing them up, such as using parseInt on "3.14", gives you the wrong result for the data. Match the method to the type you actually need.
Common Misconceptions
- Misconception: Wrapper objects are mutable.Actually: Wrapper objects are not mutable. You cannot change the value inside an existing
IntegerorDouble; you make a new object instead. - Misconception: You have to type autoboxing and unboxing conversions yourself.Actually: Autoboxing and unboxing are not something you type. The compiler inserts these conversions automatically based on the types involved.
- Misconception:
Integer.parseIntandDouble.parseDoublereturn wrapper objects.Actually:Integer.parseIntandDouble.parseDoublereturn primitives, not wrapper objects.parseIntreturns anintandparseDoublereturns adouble. - Misconception:
IntegerandDoublerequire an import.Actually:IntegerandDoubledo not require an import. They are part ofjava.lang, which is always available, unlikeArrayListfromjava.util. - Misconception: Storing numbers in an
ArrayListstores primitives.Actually: Storing numbers in anArrayListdoes not store primitives. The list holdsIntegerorDoubleobjects, and autoboxing is what lets you add plain numbers to it.
Related AP Computer Science A Guides
Practice Questions for 4.7
1 of 10
Consider the following code segment.
</>JavaString text = "17"; Integer num = Integer.parseInt(text); Double half = num / 2.0; Integer rem = num % 5; num = num + rem; System.out.print(half + " and " + num);
What is printed as a result of executing this code segment?
A.
8.0 and 19
B.
8.5 and 17
C.
8.5 and 19
D.
8.0 and 17
Vocabulary
The following words are mentioned explicitly in the AP® course framework for this topic.Term | Definition |
|---|---|
autoboxing | The automatic conversion by the Java compiler from a primitive type to its corresponding wrapper class object, such as converting an int to an Integer or a double to a Double. |
Double class | A wrapper class in the java.lang package that represents a primitive double value as an object; Double objects are immutable. |
immutable | A property of String objects meaning that once created, their content cannot be changed; methods called on a String return a new String rather than modifying the original. |
Integer class | A wrapper class in the java.lang package that represents a primitive int value as an object; Integer objects are immutable. |
parseDouble | A static method of the Double class that converts a String argument to a double value. |
parseInt | A static method of the Integer class that converts a String argument to an int value. |
primitive type | A basic data type in Java such as int or double that is not an object. |
unboxing | The automatic conversion by the Java compiler from a wrapper class object to its corresponding primitive type, such as converting an Integer to an int or a Double to a double. |
wrapper class | A class that encapsulates a primitive data type and provides object-oriented functionality, such as Integer and Double. |
See Fiveable more often in Google
Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.