Sub topic 4
Storing Numbers: Wrapper Types and Autoboxing
Collections hold objects. But many values you work with are numbers, such as scores, prices, or counts. In Java, plain numbers like int are not objects. This chapter shows how Java lets you store numbers in a collection anyway, and covers one mistake that catches many beginners.
Primitives and Wrapper Types
A primitive is a basic value type built into Java, such as int, double, or boolean. A primitive holds only a raw value. It has no methods, and it is not an object.
A wrapper type is a class that holds one primitive value inside an object. Each primitive has a matching wrapper type.
| Primitive | Wrapper type |
|---|---|
| `int` | `Integer` |
| `double` | `Double` |
| `long` | `Long` |
| `boolean` | `Boolean` |
| `char` | `Character` |
The type parameter of a generic type must be a class. That means List<int> does not compile. You write List<Integer> instead.
Autoboxing and Unboxing
Autoboxing is Java's automatic conversion of a primitive into its wrapper type. Unboxing is the reverse: Java pulls the primitive value back out of the wrapper. Both happen without any extra code from you.
Here is what that looks like in code:
import java.util.ArrayList;
import java.util.List;
public class AutoboxDemo {
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>();
scores.add(90); // int 90 is boxed into an Integer
scores.add(75);
int first = scores.get(0); // Integer is unboxed into an int
int total = first + scores.get(1);
System.out.println(total); // 165
}
}You pass plain numbers to add, and you do math with the results of get. Java takes care of the conversions.
The remove Trap
List has two remove methods:
remove(int index)removes the element at that position.remove(Object o)removes the first element equal to that value.
With a List<String>, you can't mix them up. With a List<Integer>, you can. When you write nums.remove(1), the 1 is an int. Java picks the method that takes an int without boxing, so it removes by index.
To remove by value, pass an Integer object:
import java.util.ArrayList;
import java.util.List;
public class BoxingDemo {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(10, 20, 30));
nums.remove(1); // removes the element at index 1
System.out.println(nums); // [10, 30]
nums.remove(Integer.valueOf(10)); // removes the value 10
System.out.println(nums); // [30]
}
}Integer.valueOf(10) creates an Integer object, so Java calls remove(Object).
Exercise
Why does List<int> ages = new ArrayList<>(); fail to compile, and how do you fix it?
Show answer
A generic type parameter must be a class, and int is a primitive. Use the wrapper type instead: List<Integer> ages = new ArrayList<>();.
Exercise
Given List<Integer> nums = new ArrayList<>(List.of(5, 1, 3));, what does nums contain after nums.remove(1);? What call would remove the value 1 instead?
Show answer
nums.remove(1) removes by index, so it deletes the element at position 1 (the value 1), leaving [5, 3]. In this list the result happens to look the same either way. To be sure you remove by value, call nums.remove(Integer.valueOf(1));.