0

Here I have a question regarding Java generics. Assuming we have List data structure in the following forms:

List<Object>
List<?>
List<T>
List<E>

So, what are the differences amongst those 4 forms? Best regards

ucas
  • 417
  • 1
  • 11
  • 30

1 Answers1

10

The only difference between List<T> and List<E> is that they use a different name. In both cases, it's a type variable that has to be defined somewhere else.

List<?> indicates that the list has some specific, but unknown, element type. It could be a List<String>, a List<Integer>, or a List<DeliciousPie>. You can't add any element except null to a List<?>, because you don't know if that object has the same type as the list's element type.

A List<Object> is a list that can contain any object. You can add any element to it.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413