This passes compilation:
LinkedList<?> list1 = new LinkedList<String> ();
This doesn't:
LinkedList<Object> list2 = new LinkedList<String> ();
i.e. a LinkedList<?>
variable can be assigned any LinkedList<SomeType>
. A LinkedList<Object>
variable can only be assigned a LinkedList<Object>
(or a raw LinkedList
, which is not advised to use).
On the other hand the following add
:
LinkedList<?> list1 = new LinkedList<String> ();
list1.add("x");
doesn't pass compilation, while the following does:
LinkedList<Object> list2 = new LinkedList<Object> ();
list2.add("x");