So I was reading up on generics to re-familiarize myself with the concepts, especially where wildcards are concerned as I hardly ever use them or come across them. From the reading I've done I can not understand why they use wildcards. One of the examples I keep coming across is the following.
void printCollection( Collection<?> c ) {
for (Object o : c){
System.out.println(o);
}
}
Why would you not write this as:
<T> void printCollection( Collection<T> c ) {
for(T o : c) {
System.out.println(o);
}
}
Another example from the oracle website:
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
Why is this not written as
public static <T extends Number> double sumOfList(List<T> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
Am I missing something?