4

When I try to assign an object of type List<List<Object>> to a variable with a wildcard parameterized type List<List<?>>, I get the error "incompatible types: List<List<Object>> cannot be converted to List<List<?>>".

Example:

List<List<Object>> x = new ArrayList<>();
List<List<?>> y = x;   // Error

On the other hand, assigning an object of type List<Object> to a variable of type List<?> work perfectly fine.

Example:

List<Object> x = new ArrayList<>();
List<?> y = x;   // Works fine

Why is that? Why does it work for List<?> but not for List<List<?>>?

Environment: JDK 7, Netbeans 8

Florian Fankhauser
  • 3,615
  • 2
  • 26
  • 30
  • Thanks for the hint. Sorry, that I didn't find this by myself. http://stackoverflow.com/a/3547372/104976 perfectly answers my question. – Florian Fankhauser Jun 18 '14 at 08:40
  • No problem! No need to apologize. I had just seen it before so I knew what I was looking for. – awksp Jun 18 '14 at 08:41

0 Answers0