The java documentation explains the difference between using Object as a type parameter and using and unbounded wildcard (?) by using the following code example:
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
and
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
It says that the first example fails to achieve its goal of printing a list of any type. It can only print lists of Objects; it cannot print List<Integer>
, List<String>
, List<Double>
, and so on, because they are not subtypes of List<Object>
.
But surely Integer, String and Double are subtypes of Object? Doesn't everything inherit from Object in Java?