58

Possible Duplicate:
What does List<?> mean in java generics?
What does the question mark in Java generics' type parameter mean?

Apologies but it was difficult trying to search for <?>.

What does mean in regards to Java generics? I understand <A extends B> and <A super B>, but I have never seen this question mark on its own before.

Community
  • 1
  • 1
intrigued_66
  • 16,082
  • 51
  • 118
  • 189

4 Answers4

73

<?> is a shorthand for <? extends Object>, it's also known as an unbounded wildcard. So you can specify any type of object in your generic.

For example the List class is declared as List<?>, because it can be a list of anything you want.


Resources:

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 2
    if anything? then why you are using generic? I don't think so it's good answer to make difference. Because `` will also allow you to specify any type of object. – Asif Mushtaq Apr 07 '16 at 09:34
  • I think this is not true. It depends on the type definition itself. E.g `A`, then `A>` means `anything that extends B`. So `>` means "anything that extends the defined parameter of the type". – Stuck Jan 07 '21 at 08:43
16

Its a wildcard type. It is short for ? extends Object

If you get it, all you know is its an Object. If you try to set, you can't because it could be any sub class of Object.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 7
    What's the difference between `Class>` and 'Class'? Why would I ever need to use `Class>` instead of `Class`? – ycomp Oct 22 '15 at 18:37
  • 2
    @ycomp a raw type has generic turned off. For example. Map map; map.entrySet() is a plain set, not a `Set` whereas use `Map, ?>` and you entrySet is a `Set>` – Peter Lawrey Oct 23 '15 at 12:48
  • Why not just use Object in that case? What is the advantage of using generics? – Davor Mar 23 '16 at 12:07
  • @Davor `` and `>` are not the same. The wildcard means the type is unknown. `` means you can pass an Object or a sub-class. – Peter Lawrey Mar 23 '16 at 14:01
  • A quick question. In the case of `Map, ?>`, does the class of the second `?` has to be the same of the first one? – hackjutsu Jun 10 '17 at 17:45
  • 2
    @hackjutsu No, one `?` is never assumed to be the same as another `?` – Peter Lawrey Jun 18 '17 at 19:22
5

The wildcard Generic is "something". It will be handled as something that extends from Object.

From Java documentation:

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

So, for instance, a List<?> is a list containing objects from unknown type.

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
3

Its a wildcard. Suppose that you have a collection but the type of that collection is not known, thus you denote it by "?". It simply specifies that the type is not known. For more details refer http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf I'm sure it will help.

praxmon
  • 5,009
  • 22
  • 74
  • 121
  • is this valid? "ArrayList> list = document.getEmbedded(l, ArrayList.class);" Will I be able to treat "list" as a String array? (document is bson) – Tony Jul 26 '23 at 14:22