The sentence that confuses you is trying to warn you that although List<?> Is a supertype of all shared lists, you cannot add anything to the List<?> Collection.
Suppose you tried the following code:
private static void addObjectToList1(final List<?> aList, final Object o ) { aList.add(o); } private static void addObjectToList2(final List<Object> aList, final Object o ) { aList.add(o); } private static <T> void addObjectToList3(final List<T> aList, final T o ) { aList.add(o); } public static void main(String[] args) { List<String> testList = new ArrayList<String>(); String s = "Add me!"; addObjectToList1(testList, s); addObjectToList2(testList, s); addObjectToList3(testList, s); }
addObjectToList1 does not compile because you cannot add anything but null to List<?> . (This is what the sentence is trying to tell you.)
addObjectToList2 compiles, but calling it in main() does not compile, because List<Object> not a supertype of List<String> .
addObjectToList3 compiles and calls. This is a way to add items to the general list.
source share