What's the Difference Between <> and <? Extends Object> in Java Generics

What's the difference between ? and ? extends Object in Java Generics?

<?> and <? extends Object> are synonymous, as you'd expect.

There are a few cases with generics where extends Object is not actually redundant. For example, <T extends Object & Foo> will cause T to become Object under erasure, whereas with <T extends Foo> it will become Foo under erasure. (This can matter if you're trying to retain compatibility with a pre-generics API that used Object.)

Source: http://download.oracle.com/javase/tutorial/extra/generics/convert.html; it explains why the JDK's java.util.Collections class has a method with this signature:

public static <T extends Object & Comparable<? super T>> T max(
Collection<? extends T> coll
)

What is the difference between T and T extends Object in java?

There's no difference. <T> and <T extends Object> are equivalent.

What is the difference between ListSomething and List? extends Something?

List<Something> is a list of objects of type Something.
List<? extends Something> is a list of objects of some particular type which extends Something.

So, List<Object> can have objects of any class that extends Object.
But List<? extends Object> can only be initialised to or assigned as a List of a objects of a particular class that extends Object

When is this useful? Say you want to write a method that takes a List of Objects and prints each item:

void print(List<Object> list) {
for (Object obj: list) {
System.out.println(obj)
}
}

Now, let's say you have a List<Integer>. You cannot pass it to the above method, because print takes List<Object>, and List<Integer> cannot be assigned to a List<Object>. To get around this, we redefine print as:

void print2(List<? extends Object> list) {
for (Object obj: list) {
System.out.println(obj)
}
}

Now, we can pass List of any subclass of Object to print2. print2 will accept List<Integer>, List<String> etc.

On the flip side, you cannot add anything to the list inside print2, because print2 does not know the concrete subtype of Object which is used. Hence you can use ? extends ... only in methods where you do not have to add anything to the List.

Difference between ? super T and ? extends T in Java

extends

The wildcard declaration of List<? extends Number> foo3 means that any of these are legal assignments:

List<? extends Number> foo3 = new ArrayList<Number>();  // Number "extends" Number (in this context)
List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number
List<? extends Number> foo3 = new ArrayList<Double>(); // Double extends Number
  1. Reading - Given the above possible assignments, what type of object are you guaranteed to read from List foo3:

    • You can read a Number because any of the lists that could be assigned to foo3 contain a Number or a subclass of Number.
    • You can't read an Integer because foo3 could be pointing at a List<Double>.
    • You can't read a Double because foo3 could be pointing at a List<Integer>.
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:

    • You can't add an Integer because foo3 could be pointing at a List<Double>.
    • You can't add a Double because foo3 could be pointing at a List<Integer>.
    • You can't add a Number because foo3 could be pointing at a List<Integer>.

You can't add any object to List<? extends T> because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object is allowed in that List. The only "guarantee" is that you can only read from it and you'll get a T or subclass of T.

super

Now consider List <? super T>.

The wildcard declaration of List<? super Integer> foo3 means that any of these are legal assignments:

List<? super Integer> foo3 = new ArrayList<Integer>();  // Integer is a "superclass" of Integer (in this context)
List<? super Integer> foo3 = new ArrayList<Number>(); // Number is a superclass of Integer
List<? super Integer> foo3 = new ArrayList<Object>(); // Object is a superclass of Integer
  1. Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from List foo3:

    • You aren't guaranteed an Integer because foo3 could be pointing at a List<Number> or List<Object>.
    • You aren't guaranteed a Number because foo3 could be pointing at a List<Object>.
    • The only guarantee is that you will get an instance of an Object or subclass of Object (but you don't know what subclass).
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:

    • You can add an Integer because an Integer is allowed in any of above lists.
    • You can add an instance of a subclass of Integer because an instance of a subclass of Integer is allowed in any of the above lists.
    • You can't add a Double because foo3 could be pointing at an ArrayList<Integer>.
    • You can't add a Number because foo3 could be pointing at an ArrayList<Integer>.
    • You can't add an Object because foo3 could be pointing at an ArrayList<Integer>.

PECS

Remember PECS: "Producer Extends, Consumer Super".

  • "Producer Extends" - If you need a List to produce T values (you want to read Ts from the list), you need to declare it with ? extends T, e.g. List<? extends Integer>. But you cannot add to this list.

  • "Consumer Super" - If you need a List to consume T values (you want to write Ts into the list), you need to declare it with ? super T, e.g. List<? super Integer>. But there are no guarantees what type of object you may read from this list.

  • If you need to both read from and write to a list, you need to declare it exactly with no wildcards, e.g. List<Integer>.

Example

Note this example from the Java Generics FAQ. Note how the source list src (the producing list) uses extends, and the destination list dest (the consuming list) uses super:

public class Collections { 
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
}

Also see
How can I add to List<? extends Number> data structures?

Java Generics: Special usage T extends Object & Interface

<T extends Object & Interface>

That Object is clearly redundant and typically equals to

<T extends Interface>

Note that using Interface as a class name is highly discouraged.

What is the difference between ? and Object in Java generics?

An instance of HashMap<String, String> matches Map<String, ?> but not Map<String, Object>. Say you want to write a method that accepts maps from Strings to anything: If you would write

public void foobar(Map<String, Object> ms) {
...
}

you can't supply a HashMap<String, String>. If you write

public void foobar(Map<String, ?> ms) {
...
}

it works!

A thing sometimes misunderstood in Java's generics is that List<String> is not a subtype of List<Object>. (But String[] is in fact a subtype of Object[], that's one of the reasons why generics and arrays don't mix well. (arrays in Java are covariant, generics are not, they are invariant)).

Sample:
If you'd like to write a method that accepts Lists of InputStreams and subtypes of InputStream, you'd write

public void foobar(List<? extends InputStream> ms) {
...
}

By the way: Joshua Bloch's Effective Java is an excellent resource when you'd like to understand the not so simple things in Java. (Your question above is also covered very well in the book.)

Java Generics WildCard: ? extends Number vs T extends Number

There is no difference in this case, because T is never used again.

The reason for declaring a T is so that you can refer to it again, thus binding two parameter types, or a return type together.

difference between ? and ? extends object

They both are synonymous. So there is no difference

Also check this out:- Converting Legacy Code to Use Generics

Also as notnoop answered in this question there is a very minor difference between the two:

  1. The JVMS (Java Virtual Machine Specification) has a special specification for the unbounded wildcards, as ClassFileFormat-Java5
    specifies that unbounded wildcard gets encoded as *, while encodes a
    Object-bounded wildcard as +Ljava/lang/Object;. Such change would leak
    through any library that analyzes the bytecode. Compiler writers would
    need to deal with this issue too. From revisions to "The class File
    Format"

  2. From reifiablity standpoint, those are different. JLS 4.6 and 4.7 codify List<?> as a reifiable type, but List<? extends Object> as a
    erasured type. Any library writer adding .isReifiable() (e.g. mjc lib)
    needs to account for that, to adhere to the JLS terminology. From JLS
    4.6 and 4.7.



Related Topics



Leave a reply



Submit