114

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
orbfish
  • 7,381
  • 14
  • 58
  • 75
  • 2
    It's the same thing. See http://stackoverflow.com/questions/2274720/java-extends-object-question – Dan Nov 08 '11 at 18:34
  • 4
    @Dan If you search "? extends Object" in that question you don't find anything. I'm reading through the answers to see if I can infer anything but I don't think this is it. Specifically, it's not talking about generics. – orbfish Nov 08 '11 at 18:38
  • @Dan - That's a different question. I have seen this question before and I remember at least a mention of a subtle difference. Let me see if I can find it.. – Paul Bellora Nov 08 '11 at 18:42
  • It's also not this one if you find it: http://stackoverflow.com/questions/678822/what-is-the-difference-between-and-object-in-java-generics – orbfish Nov 08 '11 at 18:42
  • 1
    Here we go: possible duplicate of [Unbounded wildcards in Java](http://stackoverflow.com/questions/2016017/unbounded-wildcards-in-java) (Featuring an incorrect answer by Kevin Bourrillion no less.) – Paul Bellora Nov 08 '11 at 18:49
  • @KublaiKhan hmm the accepted answer talks about how this should not be used, rather than why you would use the syntax, so I didn't find it satisfying. The notnoop answer has good information, but I'm curious how reifiability would affect the usage in this case. Someone pins that down they get my point and will make this a unique question ;) – orbfish Nov 08 '11 at 19:13
  • I'm open to changing the green checkbox if someone comes up with the actual answer. – orbfish Nov 16 '11 at 17:06

3 Answers3

118

<?> 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
)
Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 4
    I'm unclear how the example relates? (as it uses neither > nor extends Object>) – orbfish Nov 08 '11 at 19:11
  • 30
    @orbfish: It relates only in that I thought you would find it interesting, as it's an example where `extends Object` actually *is* meaningful. If I was mistaken, then I apologize. Hopefully it will be of interest to other people coming across your question, at least. – ruakh Nov 08 '11 at 21:52
  • @ruakh - Was interested in bounded wildcards rather than type parameters, but, at least, you answered. – orbfish Nov 11 '11 at 20:59
  • 1
    You should have stated that in your original post then. That way, his answer could have been more relevant to you. – liltitus27 Nov 07 '13 at 16:11
21

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

  • How can List> be reifiable if compiler will translate it to List so information about what is the type of ? is lost? – Trismegistos Jun 09 '17 at 09:18
  • What I believe > is more designed for backward compatible with legacy APIs. to distinguish with and extends Object> which are post Java 5 code. – Dennis C Jul 10 '17 at 10:41
15

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

roottraveller
  • 7,942
  • 7
  • 60
  • 65
  • 1
    "So It can accept of Type which are not inherited from Object class", but all Java classes are inherited or implicitly inherited from the `Object` class. – hackjutsu Jan 26 '19 at 04:18