Java Generics: Wildcard capture misunderstanding

前端 未结 6 612
醉话见心
醉话见心 2020-12-01 12:27

Reading the Java online tutorial I haven\'t understand anything about wildcard capture. For example:

    import java.util.List;
    public class WildcardErro         


        
6条回答
  •  时光取名叫无心
    2020-12-01 13:16

    I guess your misunderstanding of the restriction comes from substitution of ? for any type, Object or something like that in your mind. But that assumption is not correct, ? in fact means unknown type. So in the following line

    fooz.set(0, foo);
    

    you're trying to assign the variable of some type to the variable of unknown type (since the function signature is like void set(int, ?)), which can never be possible, whatever the type of foo would be. In your case the type of foo is Object and you cannot assign it to a variable of some unknown type, which in fact may be Foo, Bar or any other.

提交回复
热议问题