Is `ArrayList<Integer>` a subtype of `List<Integer>`, `Collection<Integer>`, `List<Number>`, `List<? extends Number>` and `List<?>`, and which Java rule decides each?
answer
- two routes: hierarchy, then type arguments
- substitute E up the declarations
- containment compares type arguments
- a plain type argument contains only itself
- the wildcard's bound decides
basics
~10 sArrayList<Integer> is a subtype of all of them except List<Number>. Inheritance carries Integer up to List and Collection, and both wildcards contain Integer, but the plain type argument Number contains only itself.
solid answer
~40 s`ArrayList<Integer>` is a subtype of `List<Integer>`, `Collection<Integer>`, `List<? extends Number>` and `List<?>`, and not of `List<Number>`. JLS 4.10.2 gives two routes. The first is inheritance: `ArrayList<E>` implements `List<E>`, so replacing `E` with `Integer` makes `List<Integer>` a supertype, and `Collection<Integer>` follows the same way further up. The second is containment: `C<S>` is a supertype of `C<T>` when the type argument `S` contains `T`, and JLS 4.5.1 says `? extends Number` contains `Integer` because `Integer` extends `Number`, while `?` contains every reference type. `Number` as a plain type argument contains only `Number`, so `List<Number>` fails even though `Integer` extends `Number`. That invariance protects the list: a `List<Number>` view of an `ArrayList<Integer>` would accept a `Double`.
code
java · 13 linesimport java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class SubtypeCheck {
static void assign(ArrayList<Integer> ints) {
List<Integer> a = ints; // inheritance, E := Integer
Collection<Integer> b = ints; // inheritance, transitively
List<? extends Number> c = ints; // ? extends Number contains Integer
List<?> d = ints; // ? contains every reference type
// List<Number> e = ints; // error: incompatible types
}
}go deeper
Recall the headline: a List<Integer> is not a List<Number>, even though Integer extends Number, while List<? extends Number> and List<?> both accept it.
Explain the two mechanisms: inheritance substitutes the type argument up the hierarchy, and containment compares type arguments on one generic type. Derive why ? extends Number contains Integer.
Use the rules when reviewing signatures: choose parameter types callers can actually pass, and read an incompatible-types error on nested generics by finding which type argument is plain and must match exactly.
Weigh what a team standardises for public signatures: wildcards widen who can call an API at the price of noisier declarations, and tightening a published parameter type later breaks the callers who relied on it.
## The five verdicts A **subtype** relation `S <: T` means a value of type `S` can be used where `T` is expected without a cast, because assignment allows a widening reference conversion from a subtype. A **parameterized type** such as `List<Integer>` is a generic type plus its **type argument**, here `Integer`. The Java Language Specification decides all five cases with two rules: one moves up the class hierarchy, the other compares type arguments. | Is `ArrayList<Integer>` a subtype of | Verdict | Rule that decides it | |---|---|---| | `List<Integer>` | Yes | Inheritance: `ArrayList<E>` implements `List<E>`, with `E` replaced by `Integer` | | `Collection<Integer>` | Yes | Inheritance, transitively, through `List<Integer>` | | `List<Number>` | No | `Number` as a plain type argument contains only `Number` | | `List<? extends Number>` | Yes | `? extends Number` contains `Integer`, because `Integer` extends `Number` | | `List<?>` | Yes | `?` contains every reference type | ## Rule one: inheritance carries the type argument JLS 4.10.2 lists the direct supertypes of a parameterized type `C<T1,...,Tn>`: - its direct superclass type, with each type parameter replaced by the matching type argument; - its direct superinterface types, with the same replacement; - every `C<S1,...,Sn>` where each `Si` **contains** `Ti`; - `Object`, if `C` is an interface with no direct superinterface; - the raw type `C`. Supertypes are the reflexive and transitive closure of that relation. `ArrayList` is declared as `ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable`, so `ArrayList<Integer>` has `AbstractList<Integer>` and `List<Integer>` as direct supertypes. `List<E>` extends `SequencedCollection<E>` since Java 21, and that extends `Collection<E>`, so `Collection<Integer>` follows. `Integer` passes through unchanged at every step, because each declaration hands `E` straight to its parent. ## Rule two: containment compares type arguments When the generic type is the same on both sides, only the type arguments differ, and JLS 4.5.1 defines the test. A type argument `T1` **contains** `T2`, written `T2 <= T1`, when the set of types `T2` denotes is provably a subset of the set `T1` denotes. Four of its rules matter here: - `T <= T` - `T <= ? extends T` - `? extends T <= ? extends S` if `T <: S` - `? extends T <= ?` Containment is reflexive and transitive too, so the two wildcard verdicts are short derivations. `Integer <= ? extends Integer` and `? extends Integer <= ? extends Number` chain to `Integer <= ? extends Number`. The same first link with the fourth rule gives `Integer <= ?`. So `List<? extends Number>` and `List<?>` are direct supertypes of `List<Integer>`, and `ArrayList<Integer>` reaches them through it. No cast, unchecked conversion or erasure is involved. ## Why `List<Number>` is refused The type argument `Number` is not a wildcard, so the only rule that applies is `T <= T`: it contains `Number` and no other type. `Integer <: Number` relates the element types and does not carry over to the lists. This is **invariance**, and it keeps the list sound: ```java ArrayList<Integer> ints = new ArrayList<>(); List<Number> nums = ints; // compile error; if it compiled: nums.add(3.14); // a Double in a list of Integer Integer first = ints.get(0); // ClassCastException ``` The wildcard form is safe because it gives up what made this unsound: `add` through `List<? extends Number>` is refused for everything but `null`, which belongs with the wildcard's write restrictions. **Arrays** took the opposite decision. JLS 4.10.3 makes `Integer[]` a subtype of `Number[]`, and the hole is closed at run time: storing a `Double` through the `Number[]` reference throws `ArrayStoreException`. A list carries no element type at run time, so it has no such store check. The JLS rules the generic case out at compile time: JLS 4.10 states that `T <: S` does not imply `C<T> <: C<S>`. ## The same rules at the edges - **Nesting.** `List<List<Integer>>` is not a subtype of `List<List<? extends Number>>`. The wildcard sits inside the outer type argument, so the outer argument is a plain type and must match exactly. `List<? extends List<? extends Number>>` is a supertype, because a top-level wildcard does the comparing. - **Direction.** `ArrayList<Number>` is not a subtype of `List<? extends Integer>`, because `Number` does not extend `Integer`. - **Lower bounds.** `List<? super Integer>` is also a supertype of `ArrayList<Integer>`, by the rule `T <= ? super T`. - **Raw types.** The raw `List` is a supertype too, by the last row of the list above. The JLS allows raw types only as a concession to legacy code. - **Back down.** `List<? extends Number>` is not a subtype of `List<Integer>` or `List<Number>`, so assigning it to either does not compile. ## Deciding any such case To decide whether `A<X>` is a subtype of `B<Y>`: 1. Walk up from `A<X>` through the declarations, substituting as you go, until you reach `B`. If you never reach it, the answer is no. 2. Compare what you got with `Y`. If `Y` is a plain type, the two must be the same type. If `Y` is a wildcard, apply its bound. The specification lets the two steps happen in either order, because the relation is transitive; this order is simply easier by hand.
- Why is `List<List<Integer>>` not a subtype of `List<List<? extends Number>>`?The outer type argument `List<? extends Number>` is a plain type with a wildcard inside it, not a wildcard, so only `T <= T` applies and `List<Integer>` is a different type. Allowing it would let code add a `List<Double>` through the outer list. The supertype you want is `List<? extends List<? extends Number>>`, whose top-level wildcard contains `List<Integer>`.
- Is `List<? extends Number>` a subtype of `Collection<? extends Number>`, given that its own type argument is a wildcard?Yes. JLS 4.10.2 takes the direct supertypes of a wildcard-parameterized type from its capture: `List<? extends Number>` captures to `List<CAP>`, with `CAP` a fresh type variable bounded by `Number`. Its supertypes include `Collection<CAP>`, and `? extends Number` contains `CAP`, so the assignment compiles without a cast.
- Why does `Number[] n = new Integer[1]` compile when `List<Number> n = new ArrayList<Integer>()` does not?JLS 4.10.3 makes arrays covariant, so `Integer[]` is a subtype of `Number[]`. An array keeps its element type at run time, so a bad store throws `ArrayStoreException`. A list carries no element type at run time and has no such store check, and JLS 4.10 states that `T <: S` does not imply `C<T> <: C<S>`, so the list assignment is a compile-time error.
A crate labelled apples cannot be relabelled fruit, because someone trusting the new label would drop in a pear. A label reading some kind of fruit, unknown which, is fine: nobody can safely add to it, yet whatever comes out is fruit.
- List<Integer> is a List<Number> because Integer extends Number.
- ArrayList<Integer> fits List<Object> as well as List<?>, since both accept anything.
- Passing ArrayList<Integer> as List<? extends Number> relies on an unchecked conversion.
- List<Number> is refused because of erasure, not because of a subtyping rule.
- Collection<Integer> needs a wildcard, because List and Collection are different generic types.
- List<List<Integer>> is a subtype of List<List<? extends Number>> because the inner wildcard accepts Integer.
- Wildcard type arguments
- Java 1.5
Assumes Java 27; since Java 21 List reaches Collection through SequencedCollection, which changes none of the verdicts, and Java 25 has the same declarations.
context
- Chapter 4. Types, Values, and Variablesdocs.oracle.com
- ArrayList (Java SE 27 & JDK 27)docs.oracle.com