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

JavaSyntaxGenerics

Java Problem Overview


I've seen the wildcard used before to mean any object - but recently saw a use of:

<? extends Object>

Since all objects extend Object, are these two usages synonymous?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

Although <?> is supposed to be a shortcut for <? extend object>, there is a tiny difference between the two. <?> is reifiable while <? extend object> is not. The reason they did this is to make it easier to distinguish reifiable type. Anything that looks like <? extends something>,<T>,<Integer> are nonreifiable.

For example, this code would work

List aList = new ArrayList<>();
boolean instanceTest = aList instanceof List<?>;

but this gives an error

List aList = new ArrayList<>();
boolean instancetest = aList instanceof List<? extends Object>;

for more info read Java generics and collections by Maurice Naftalin

Solution 3 - Java

<?> is a shorthand for <? extends Object>. You may read below shared link for more details.


<?>

"?" denotes any unknown type, It can represent any Type at in code for. Use this wildcard if you are not sure about Type.

ArrayList<?> unknownList = new ArrayList<Number>(); //can accept of type Number
unknownList = new ArrayList<Float>(); //Float is of type Number

Note: <?> means anythings. So It can accept of Type which are not inherited from Object class.

<? extends Object>

<? extends Object> means you can pass an Object or a sub-class that extends Object class.

ArrayList<? extends Number> numberList = new ArrayList<Number>(); //Number of subclass
numberList = new ArrayList<Integer>(); //Integer extends Number
numberList = new ArrayList<Float>(); // Float extends Number 

enter image description here

T – used to denote type
E – used to denote element
K – keys
V - values
N – for numbers
Ref:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionorbfishView Question on Stackoverflow
Solution 1 - JavaruakhView Answer on Stackoverflow
Solution 2 - JavaRenzhentaxi BaerdeView Answer on Stackoverflow
Solution 3 - JavaroottravellerView Answer on Stackoverflow