Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java unbound wildcard <?> vs <T> [duplicate]

Tags:

java

generics

I am confusing that if the following method are same? Are there any subtle differences?

Your advice are very appreciated.

method 1

public static double sumOfList(List<?> list) {}

method 2

public static <T> double sumOfList(List<T> list) {}
like image 934
user2761885 Avatar asked Dec 20 '22 00:12

user2761885


1 Answers

Inside the method you can use T in second case .

You cannot in first case.

Consider this example

private static <T> void printList(List<T> list) {
for (T t: list) {
    System.out.println(t);
}

}

like image 151
Suresh Atta Avatar answered Jan 06 '23 12:01

Suresh Atta