A
A
Alexander2014-04-19 17:42:25
Java
Alexander, 2014-04-19 17:42:25

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;
    }
and
public static <T  extends Number> double sum(List<T> list){
        double sum = 0;
        for(Number n : list){
            sum += n.doubleValue();
        }
        return sum;
    }

Aren't these two methods equivalent?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Vershinin, 2014-04-19
@samrus

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 question

Ask a Question

731 491 924 answers to any question