Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between List<Object> and List<?>

Tags:

java

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.

like image 894
Aldsjers Dostrnik Avatar asked Sep 09 '12 16:09

Aldsjers Dostrnik


People also ask

What Does list <?> Mean in Java?

In Java, a list interface is an ordered collection of objects in which duplicate values can be stored. Since a List preserves the insertion order, it allows positional access and insertion of elements. List interface is implemented by the following classes: ArrayList. LinkedList.

What means list object?

In category theory, an abstract branch of mathematics, and in its applications to logic and theoretical computer science, a list object is an abstract definition of a list, that is, a finite ordered sequence.

What is the difference between ArrayList and ArrayList <?> In Java?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.


2 Answers

Explanation:

? is a "wildcard". You can use it in different ways:

unbounded wildcard:

?  

wildcard with upper bound:

? extends Object (or another class) 

wildcard with lower bound

? super Object (or another class) 

Examples:

List <?> list; 

you can assign to list any List with any type parameter.

List<? extends Number> list; 

you can assign to list any List with a type parameter Number (or Integer, Float, ecc)

List<? super Integer> list; 

you can assign to list any List with a type parameter Integer, or a type in the type gerarchy of Integer(Number,Comparable, Serializable, Object ...)

Summary:

So the declaration

List<Object> list; 

is similar to

List<? super Object> list; 

because you can assign to "list" only a List of Objects; then you can use the list in this way:

    list = new ArrayList<Object>();     list.add(new String(""));     list.add(new Integer(3)); 
like image 165
Roberto Mereghetti Avatar answered Sep 18 '22 04:09

Roberto Mereghetti


One of the major differences is how you can modify the lists. The following works well

List<Object> l1; l1.add(new Object()); // OK 

while the second case produces a compilation error.

List<?> l2; l2.add(new Object()); // Compile error 

The reasons for that I think has been debated a few times on the site already. In short,<?> means a (as in one) specific yet unknown type. Since that type is not known compile-time, you're not allowed to modify the list.

like image 40
Johan Sjöberg Avatar answered Sep 20 '22 04:09

Johan Sjöberg