What’s the difference between List and List

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));

Leave a Comment