4.7 Wrapper classesInteger and Double: numbers as objects

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
- line 1-3 Autoboxing: Java wraps the primitive in an object for you
- line 2-4 Unboxing: the object turns back into a primitive
- line 5-6 The largest and smallest int, on the Quick Reference
- line 7-8 Text to number, also on the Quick Reference
- line 10 One past the top wraps around to the bottom
| Primitive | Wrapper class | Useful on the exam |
|---|---|---|
| int | Integer | Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.parseInt(s) |
| double | Double | Double.parseDouble(s) |
| (not allowed) | ArrayList<int> | Write ArrayList<Integer> instead |
?Wait, what's overflow?
?Wait, what does immutable mean again?
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.
I thought about it. Show me
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);- a is an Integer holding 40. a / 3 unboxes it, and 40 / 3 is int division: b is 13.
- Double.parseDouble("1.5") is 1.5, and 1.5 * 13 is 19.5, so c is 19.5.
- "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. - The output is 13 19.5 85.
Answer: 13 19.5 85
Stuck? Get a hint
- 40 / 3 with ints drops the remainder.
- Multiply 1.5 by b.
- "7" + "2" is a String. What int does "72" become?
- AArrayList<int> a = new ArrayList<int>();
- BInteger x = 5;
- Cint y = Integer.parseInt("12");
- 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.