I am trying to understand why do we need the wildcard -- question mark in Java Generics, why can't we just use the normal single character T or E etc. as the type? Look at the following example:
public class App {
public static void main(String[] args) {
App a = new App();
List<String> strList = new ArrayList<String>();
strList.add("Hello");
strList.add("World");
List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
a.firstPrint(strList);
a.firstPrint(intList);
a.secondPrint(strList);
a.secondPrint(intList);
}
public <T extends Object> void firstPrint(List<T> theList) {
System.out.println(theList.toString());
}
public void secondPrint(List<? extends Object> theList) {
System.out.println(theList.toString());
}
}
The result is same albeit the wildcard version is more concise. Is that the only benefit?