Java wildcard mask is incompatible

When I try to assign an object of a type to a List<List<Object>>variable with a wildcard parameter 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 a type to a type List<Object>variable List<?>works fine.

Example:

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

Why? Why does this work for List<?>, but not for List<List<?>>?

Wednesday: JDK 7, Netbeans 8

+4
source share

Source: https://habr.com/ru/post/1545037/


All Articles