Skip to main content

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
  • Integer
  • Double
  • 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 int into an Integer (or a double into a Double). It happens when you assign a primitive to a wrapper variable, like Integer count = 7;, or pass a primitive to a method that expects the wrapper, like nums.add(8) on an ArrayList<Integer>.
  • Unboxing turns an Integer back into an int (or a Double into a double). It happens when you assign a wrapper to a primitive variable, like int 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.

  1. 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 solution
    1. Step 1: Integer count = 7; autoboxes 7 into an Integer. int plain = count; unboxes it, so plain is 7.
    2. Step 2: price * 2 unboxes 2.5 and multiplies: total is 5.0.
    3. Step 3: nums.add(plain + 1) autoboxes 8 and adds it to the list. int first = nums.get(0); unboxes it back to 8.
    4. Step 4: Printing count shows its value, 7.

    Answer: It prints 7 5.0 8.

  2. 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 solution
    1. Step 1: a + b joins two strings: "12" + "30" is "1230".
    2. Step 2: Integer.parseInt(a) is the int 12 and Integer.parseInt(b) is 30, so their sum is 42.
    3. Step 3: Double.parseDouble("1.5") is 1.5, and 1.5 × 3 = 4.5.

    Answer: Three lines: 1230, 42, 4.5.

Common mistakes

  • Writing ArrayList<int>. Type parameters must be classes, so use ArrayList<Integer>.
  • Adding two numeric strings and expecting a sum. Parse them first.
  • Calling remove(3) on an ArrayList<Integer> to remove the value 3. It removes the element at index 3.
  • Comparing two Integer objects with ==. Unbox them or use equals.

On the exam

  • Expect Integer.parseInt and Double.parseDouble in 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.

Question 1 of 4

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?

Question 2 of 4

Which of the following statements causes a compile-time error?

Question 3 of 4

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?

Question 4 of 4

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