I've read a few topics which cover certain questions about generics, such as their relationship with raw types. But I'd like an additional explanation on a certain line found in the Java SE tutorial on unbound generics .
According to a sentence :
The goal of printList is to print a list of any type, but it fails to achieve that goal — it prints only a list of Object instances; it cannot print List<Integer>, List<String>, List<Double>, and so on, because they are not subtypes of List<Object>.
If I understand well this sentence; the difference between List<?>
and List<Object>
, is that we can use the type argument List<String>
or List<Integer>
by implementing the former. While if we implement the later, we can only use the type argument List<Object>
. As if List<?>
is an upper bound to Object
namely List<? extends Object>
.
But then the following sentence confuses me, in the sense that according to what I previously understood, List<Object>
should only contain instances of the class Object
and not something else.
It's important to note that
List<Object>
andList<?>
are not the same. You can insert an Object, or any subtype of Object, into aList<Object>
. But you can only insertnull
into aList<?>
.