Sub topic 10
Why List<Integer> Is Not a List<Number>
You already know that an Integer is a Number. Integer is a subclass of Number, so you can store an Integer in a Number variable. It seems natural to expect the same for lists, but Java says no. This chapter explains the reason.
The Rule: Generic Types Are Invariant
Subtyping is the relationship that lets you use one type where another is expected. Integer is a subtype of Number.
Invariance is the rule that a generic type does not inherit the subtyping of its type argument. List<Integer> and List<Number> are two unrelated types, even though Integer and Number are related.
import java.util.ArrayList;
import java.util.List;
public class InvarianceDemo {
public static void main(String[] args) {
Number n = Integer.valueOf(5); // fine: Integer is a Number
List<Integer> ints = new ArrayList<>();
// List<Number> nums = ints; // compile error
System.out.println(n);
}
}If you remove the // from the commented line, the compiler reports "incompatible types".
Why Java Forbids It
Suppose Java allowed the assignment. Then this code would compile:
import java.util.ArrayList;
import java.util.List;
public class WhatCouldGoWrong {
public static void main(String[] args) {
List<Integer> ints = new ArrayList<>();
// Imagine this line were allowed:
// List<Number> nums = ints;
// nums.add(3.14); // a Double is a Number, so this looks fine
// Integer i = ints.get(0); // but the list holds a Double!
System.out.println(ints);
}
}The variables nums and ints would point to the same list. Through nums you could add a Double. Then ints.get(0) would hand back a Double where an Integer was promised. Generics exist to stop exactly this kind of mistake, so the compiler blocks the assignment.
Arrays Behave Differently
Arrays are covariant, which means Integer[] is a subtype of Number[]. Java checks array stores only when the program runs.
public class ArrayDemo {
public static void main(String[] args) {
Integer[] ints = new Integer[1];
Number[] nums = ints; // compiles
nums[0] = 3.14; // ArrayStoreException at runtime
}
}This program compiles and then crashes. Generics move the same error to compile time, so you find it before the program runs.
| Type | `Integer` version assignable to `Number` version? | When the error appears |
|---|---|---|
| `Integer[]` → `Number[]` | Yes | Runtime (`ArrayStoreException`) |
| `List<Integer>` → `List<Number>` | No | Compile time |
What Still Works
Invariance applies to the type argument only. The class itself can still be a subtype. ArrayList<Integer> is a List<Integer>, because ArrayList implements List and the type argument is the same.
import java.util.ArrayList;
import java.util.List;
public class StillWorks {
public static void main(String[] args) {
List<Integer> ints = new ArrayList<Integer>(); // fine: same type argument
ints.add(7);
System.out.println(ints);
}
}Exercise
Does List<Object> objs = new ArrayList<String>(); compile? Explain why or why not.
Show answer
No. Generic types are invariant, so ArrayList<String> is not a List<Object>. If it were allowed, you could add an Integer through objs to a list that should hold only strings.
Exercise
Does Collection<String> c = new ArrayList<String>(); compile?
Show answer
Yes. ArrayList implements Collection, and both sides use the same type argument, String.