How
List<?>
differs fromList<Object>
The main difference is that the first line compiles but the second does not:
List<?> list = new ArrayList<String> ();
List<Object> list = new ArrayList<String> ();
However, because you don’t know what the generic type of List<?>
is, you can’t use its parameterized methods:
List<?> list = new ArrayList<String> ();
list.add("aString"); //does not compile - we don't know it is a List<String>
list.clear(); //this is fine, does not depend on the generic parameter type
As for the difference with raw types (no generics), the code below compiles and runs fine:
List list = new ArrayList<String> ();
list.add("aString");
list.add(10);