What is the difference between
public List<?> myList;
and
public List<String> myList;
I know the latter will store a List
of myList objects (Strings) but am not sure what the first does.My guess is it will take object of any type
.But is this safe?t
Yes, its unsafe, that's why you can't add to it when using it as a parameter. You can only read from it.
List<?>
means a list typed to an unknown type. This could be aList<A>, a List<B>, a List<String>
etc.Since you do not know what type the List is typed to, you can only read from the collection, and you can only treat the objects read as being Object instances.
In this method, you are not able to add things in "elements". Only read things, because you can't know what type the elements are.