In https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html, it is suggested to implement a method
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
and then to invoce it with
List<Integer> li = Arrays.asList(1, 2, 3);
System.out.println("sum = " + sumOfList(li));
- Why is it illegal to put
public static <T> double sumOfList(List<T extends Number> list)? - Is
public static <T extends Number> double sumOfList(List<T> list)equivalent? If so, is it of less good style? - When using the aforementioned code, why is it illegal to write
System.out.println("sum = " + sumOfList<Integer>(li));?