Before are all you burn me alive, I must say that I have googling this question many times and I still can't understand the difference between List<Object> and List<?>
All books I've read say that in Java every class is implicitly a subclass of Object.
However I saw here the follwing code:
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
This code is wrong (intentionally for educational purposes) and according to the author the reason is:
[...] prints only a list of Object instances; it cannot print List<Integer>, List<String>, List<Double>, and so on, because they are not subtypes of List<Object>
The solution is:
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
As you can see, the only difference is the first line:
public static void printList(List<Object> list) {
public static void printList(List<?> list) {
This is the reason of my question: What's the difference between List<Object> and List<?> ?
After all is Object superclass of everything or not?
If someone can help me with a simple explanation (I'm new with Java) I'll appreciate.
Thank you in advance.