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

前端 未结 4 1658
执笔经年
执笔经年 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:58

    Guess: The thing representing the first ? (ArrayList) does not 'implement' ArrayList (by virtue of the double nested wildcard). I know this sounds funny but....

    Consider (for the original listing):

     void g(Class x) {} // Fail
     void g(Class x) {}  // Fail
     void g(Class

    And

    // Compiles
    public class Test{
        > void f(ArrayList x) {}
        void g(ArrayList> x) {}
    
        void d(){
            ArrayList> d = new ArrayList>();
            f(d);
            g(d);
        }
    }
    

    This

    // Does not compile on g(d)
    public class Test{
        > void f(ArrayList x) {}
        void g(ArrayList> x) {}
    
        void d(){
            ArrayList d = new ArrayList();
            f(d);
            g(d);
        }
    }
    

提交回复
热议问题