AP Computer Science A · Unit 4 lecture · Part 6 of 14 · 5 min

4.7 Wrapper classesInteger and Double: numbers as objects

Mr. Thompkins
Coming up next is ArrayList, and it has one rule: it can only hold objects. An int isn't an object. So how do you put numbers in a list? You wrap them.

Every primitive type has a wrapper class whose objects hold one value of that type. In this course you use two: Integer wraps an int, and Double wraps a double. Like Strings, wrapper objects are immutable: once made, the value inside never changes.

You rarely have to wrap or unwrap anything yourself, because Java does it automatically. Autoboxing turns a primitive into a wrapper object when an object is needed, as in Integer a = 17; or list.add(17);. Unboxing turns a wrapper object back into a primitive when a primitive is needed, as in int b = a + 3;.

1Integer a = 17;12int b = a + 3;23Double d = 2.5;14double e = d * 2;25int big = Integer.MAX_VALUE;36int small = Integer.MIN_VALUE;37int n = Integer.parseInt("305");48double x = Double.parseDouble("0.75");49System.out.println(b + " " + e + " " + (n + 1) + " " + x);10System.out.println(big + 1 == small);5
  1. line 1-3 Autoboxing: Java wraps the primitive in an object for you
  2. line 2-4 Unboxing: the object turns back into a primitive
  3. line 5-6 The largest and smallest int, on the Quick Reference
  4. line 7-8 Text to number, also on the Quick Reference
  5. line 10 One past the top wraps around to the bottom
output › 20 5.0 306 0.75 true
Autoboxing, unboxing and the Integer and Double members on the Quick Reference.
PrimitiveWrapper classUseful on the exam
intIntegerInteger.MAX_VALUE, Integer.MIN_VALUE, Integer.parseInt(s)
doubleDoubleDouble.parseDouble(s)
(not allowed)ArrayList<int>Write ArrayList<Integer> instead
Each primitive and its wrapper.
?Wait, what's overflow?
An int can only hold values from Integer.MIN_VALUE (-2147483648) to Integer.MAX_VALUE (2147483647). Going past either end doesn't cause an error. The value wraps around to the other end, which is why big + 1 == small prints true.
?Wait, what does immutable mean again?
An immutable object can't be changed after it's created. Strings are immutable: toUpperCase-style methods return a new String. Integer and Double are the same. a = a + 1; makes a new Integer and points a at it; the old one is untouched.

One run-time trap: a wrapper variable can be null, and unboxing null throws a NullPointerException, because there's no number inside to take out. Beyond the course, as a bonus: == on two Integer objects compares references, not values, so compare wrapped numbers after unboxing them into ints.

Pause and think
Why is Integer.MIN_VALUE a safe starting value when you're looking for the largest element of an int array?
I thought about it. Show me
No int is smaller than Integer.MIN_VALUE, so the first element you compare is always at least as big and replaces it. Unlike starting at 0, it works even when every element is negative.
Trace the conversions

What does this print?

Integer a = 40;
int b = a / 3;
double c = Double.parseDouble("1.5") * b;
int d = Integer.parseInt("7" + "2") + b;
System.out.println(b + " " + c + " " + d);
  1. a is an Integer holding 40. a / 3 unboxes it, and 40 / 3 is int division: b is 13.
  2. Double.parseDouble("1.5") is 1.5, and 1.5 * 13 is 19.5, so c is 19.5.
  3. "7" + "2" glues two Strings into "72". parseInt makes it the int 72, and 72 + 13 is 85.
    Why?
    Inside the parentheses the + is between two Strings, so it joins them. The number only appears after parseInt.
  4. The output is 13 19.5 85.

Answer: 13 19.5 85

Stuck? Get a hint
  1. 40 / 3 with ints drops the remainder.
  2. Multiply 1.5 by b.
  3. "7" + "2" is a String. What int does "72" become?
Check yourself
Which statement does NOT compile?
  1. AArrayList<int> a = new ArrayList<int>();
  2. BInteger x = 5;
  3. Cint y = Integer.parseInt("12");
  4. Ddouble d = Double.parseDouble("3");
Show the answer

A. The type in the angle brackets must be a class, so ArrayList<int> is a compile error (unexpected type). Use ArrayList<Integer>. Integer x = 5; is fine thanks to autoboxing, and both parse calls are on the Quick Reference.

  • Integer wraps an int; Double wraps a double. Both are immutable.
  • Autoboxing (int to Integer) and unboxing (Integer to int) happen automatically.
  • Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.parseInt and Double.parseDouble are on the Quick Reference.
  • Unboxing null throws a NullPointerException.