AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/4/4-7)
Unit 4 · Topic 4.7
4.7 Wrapper Classes
Integer and Double are wrapper classes: they wrap a primitive value inside an object. This topic covers why you need them (mainly for ArrayList), how Java converts back and forth automatically, and how to turn a String into a number with parseInt and parseDouble.
Key terms
- wrapper class
IntegerDouble- autoboxing
- unboxing
Integer.parseInt
Why wrapper classes exist
Some parts of Java only work with objects. The big one in this course is ArrayList, which can hold object references but not primitives. You can't make an ArrayList<int>. Instead, you use the wrapper class: ArrayList<Integer> or ArrayList<Double>.
An Integer object holds one int value, and a Double object holds one double. Both classes are in java.lang, so no import is needed.
Both are immutable: once a wrapper object is created, its value can't change. When code seems to change an Integer, Java actually makes a new object.
Autoboxing and unboxing
Java converts between primitives and wrappers automatically.
- Autoboxing turns an
intinto anInteger(or adoubleinto aDouble). It happens when you assign a primitive to a wrapper variable, likeInteger count = 7;, or pass a primitive to a method that expects the wrapper, likenums.add(8)on anArrayList<Integer>. - Unboxing turns an
Integerback into anint(or aDoubleinto adouble). It happens when you assign a wrapper to a primitive variable, likeint first = nums.get(0);, or pass a wrapper to a method that expects the primitive.
Using them in practice
Because of autoboxing and unboxing, you can mostly treat Integer like int in arithmetic: price * 2 works when price is a Double. Still, to compare the values of two wrapper objects, unbox them into primitive variables first or use equals, because == on two objects compares references (2.6).
One trap: on an ArrayList<Integer>, list.remove(1) removes the element at index 1. It does not remove the value 1, because the argument is an int, which matches the remove(int index) method.
Integer.MAX_VALUE and Integer.MIN_VALUE are the largest and smallest values an int can hold, 2147483647 and -2147483648. They're handy starting values for a min or max.
Turning strings into numbers
Two static methods on the Java Quick Reference convert text to numbers. Integer.parseInt(s) returns the String s as an int, and Double.parseDouble(s) returns it as a double. You'll use them constantly with split when reading files (4.6), since every piece of a split line is a String.
Until you parse it, a number in a string is just text. Adding two such strings joins them instead of adding them.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Boxing and unboxing
What does this code print?
Integer count = 7; int plain = count; Double price = 2.5; double total = price * 2; ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(plain + 1); int first = nums.get(0); System.out.println(count + " " + total + " " + first);Show the solutionHide the solution
- Step 1:
Integer count = 7;autoboxes 7 into anInteger.int plain = count;unboxes it, soplainis 7. - Step 2:
price * 2unboxes 2.5 and multiplies:totalis 5.0. - Step 3:
nums.add(plain + 1)autoboxes 8 and adds it to the list.int first = nums.get(0);unboxes it back to 8. - Step 4: Printing
countshows its value, 7.
Answer: It prints
7 5.0 8. - Step 1:
- Example 2
Text versus numbers
What does this code print?
String a = "12"; String b = "30"; System.out.println(a + b); System.out.println(Integer.parseInt(a) + Integer.parseInt(b)); System.out.println(Double.parseDouble("1.5") * 3);Show the solutionHide the solution
- Step 1:
a + bjoins two strings:"12" + "30"is"1230". - Step 2:
Integer.parseInt(a)is theint12 andInteger.parseInt(b)is 30, so their sum is 42. - Step 3:
Double.parseDouble("1.5")is 1.5, and 1.5 × 3 = 4.5.
Answer: Three lines:
1230,42,4.5. - Step 1:
Common mistakes
- Writing
ArrayList<int>. Type parameters must be classes, so useArrayList<Integer>. - Adding two numeric strings and expecting a sum. Parse them first.
- Calling
remove(3)on anArrayList<Integer>to remove the value 3. It removes the element at index 3. - Comparing two
Integerobjects with==. Unbox them or useequals.
On the exam
- Expect
Integer.parseIntandDouble.parseDoublein file-reading questions. Look for the step where text turns into a number.
Connected topics
Videos
Check yourself
4 questions on 4.7 Wrapper Classes. Pick an answer to see if you got it, and why.
Consider the following code segment.String a = "12";
String b = "3";
int sum = Integer.parseInt(a) + Integer.parseInt(b);
System.out.println(a + b + " " + sum);What is printed as a result of executing the code segment?
Which of the following statements causes a compile-time error?
Consider the following code segment.String price = "2.50";
double d = Double.parseDouble(price);
System.out.println(d * 2);What is printed as a result of executing the code segment?
Consider the following code segment.ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(10);
int first = list.get(0);
Integer second = list.get(1);
System.out.println(first + second + list.size());What is printed as a result of executing the code segment?
0 of 4 answered