Posts

Showing posts with the label bounded-wildcard

Upper Bounded wildcards in Java

Upper Bounded wildcards in Java I have two generic methods that calculate the sum of elements of a List. The signatures of the methods are double method1(List<? extends Number> list) - Here I am using a wildcard. double method1(List<? extends Number> list) <U extends Number> double sumOfList1(List<U> list) - Here there is a name for type parameter. <U extends Number> double sumOfList1(List<U> list) Is there any difference between these two or they're same in terms of functionality ? Using a type parameter name instead of wildcard has any advantages ? public static double sumOfList(List<? extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s; } public static <U extends Number> double sumOfList1(List<U> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s; } Thank you. 3 Answers ...