Class<? extends T
> example:
Class<? extends Number>
can be Class<Number>
, Class<Integer>
, Class<BigDecimal>
etc... In other words any Class that extends Number
class. That is checked in compile time!
On the other hand Class<? extends ?>
can be interpreted as class of type any class which extend any class and that is a nonsense.
EDIT:
You're asking for usage. Here is an example of code that uses List<? extends Number>
to calculate sum and average of all numbers in the list:
public class Help {
private static BigDecimal sum(List<? extends Number> list) {
return list == null || list.isEmpty() ?
BigDecimal.ZERO :
list.stream()
.map(n -> new BigDecimal(n.doubleValue()))
.reduce(BigDecimal::add)
.get();
}
private static BigDecimal average(List<? extends Number> list) {
return list == null || list.isEmpty() ?
BigDecimal.ZERO :
sum(list).divide(BigDecimal.valueOf(list.size()), MathContext.DECIMAL32);
}
public static void main(String[] args) {
List<Number> numbers =
List.of(
1.0D, /*double*/
2.0F, /*float*/
3L, /*long*/
new BigInteger("4"), /*BigInteger*/
new AtomicInteger(5), /*just for test*/
(int)'a' /*int*/
);
System.out.println("sun of " + numbers + " = " + sum(numbers));
System.out.println("avg of " + numbers + " = " + average(numbers));
System.out.println("sum of empty list = " + sum(List.of()));
System.out.println("avg of empty list = " + average(List.of()));
System.out.println("sum of null list = " + sum(null));
System.out.println("avg of null list = " + average(null));
}
}
I hope that you can get some basic tips from this code.