Generic method with parameters vs. non-generic method with wildcards

前端 未结 4 1659
执笔经年
执笔经年 2021-02-07 05:15

According to this entry in the Java Generics FAQ, there are some circumstances where a generic method has no equivalent non-generic method that uses wildcard types. According

4条回答
  •  悲哀的现实
    2021-02-07 05:52

    If the type parameter were a wildcard-parameterized type, then the problem does not occur:

    Class> foo = null;
    f(foo);
    g(foo);
    

    I think this is almost certainly a weird case arising out of the fact that the type of the class literal is Class, and so the type parameter in this case (ArrayList) is a raw type, and the subtyping relationship between raw ArrayList and wildcard-parameterized ArrayList is complicated.

    I haven't read the language specification closely, so I'm not exactly sure why the subtyping works in the explicit type parameter case but not in the wildcard case. It could also very well be a bug.

提交回复
热议问题