When to use generic methods and when to use wild-card?

前端 未结 9 2193
猫巷女王i
猫巷女王i 2020-11-22 10:32

I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods.

9条回答
  •  -上瘾入骨i
    2020-11-22 10:47

    One other difference which is not listed here.

    static  void fromArrayToCollection(T[] a, Collection c) {
        for (T o : a) {
            c.add(o); // correct
        }
    }
    

    But the following will result in compile time error.

    static  void fromArrayToCollection(T[] a, Collection c) {
        for (T o : a) {
            c.add(o); // compile time error
        }
    }
    

提交回复
热议问题