Java Generics WildCard: <? extends Number> vs

前端 未结 4 502
清酒与你
清酒与你 2020-12-07 09:28

What is the difference between these 2 functions?

static void gPrint(List l) {
    for (Number n : l) {
        System.out.println(n)         


        
4条回答
  •  心在旅途
    2020-12-07 09:58

    T is a bounded type, i.e. whatever type you use, you have to stick to that particular type which extends Number, e.g. if you pass a Double type to a list, you cannot pass it a Short type as T is of type Double and the list is already bounded by that type. In contrast, if you use ? (wildcard), you can use "any" type that extends Number (add both Short and Double to that list).

提交回复
热议问题