Answer the question
In order to leave comments, you need to log in
What is the difference between bounded wildcards and parameterized types in Java?
Please explain the difference between
public static double sum(List<? extends Number> list){
double sum = 0;
for(Number n : list){
sum += n.doubleValue();
}
return sum;
}
andpublic static <T extends Number> double sum(List<T> list){
double sum = 0;
for(Number n : list){
sum += n.doubleValue();
}
return sum;
}
Answer the question
In order to leave comments, you need to log in
Are equivalent. But in the general case, the use of generic wildcards is justified when our method does not depend on the type (including does not call methods that depend on the type). When we need to use a type parameter (for example, when adding elements), we use the second option.
Source: docs.oracle.com/javase/tutorial/extra/generics/wil...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question