269

This is a small snippet of code taken from some of the examples that accompany the Stanford Parser. I've been developing in Java for about 4 years, but have never had a very strong understanding of what this style of code is supposed to indicate.

List<? extends HasWord> wordList = toke.tokenize();

I'm not worried about the details of the code. What I'm confused about is what exactly the generic expression is supposed to convey, in English.

Can someone explain this to me?

1

6 Answers 6

273
? extends HasWord

means "A class/interface that extends HasWord." In other words, HasWord itself or any of its children... basically anything that would work with instanceof HasWord plus null.

In more technical terms, ? extends HasWord is a bounded wildcard, covered in Item 31 of Effective Java 3rd Edition, starting on page 139. The same chapter from the 2nd Edition is available online as a PDF; the part on bounded wildcards is Item 28 starting on page 134.

Update: PDF link was updated since Oracle removed it a while back. It now points to the copy hosted by the Queen Mary University of London's School of Electronic Engineering and Computer Science.

Update 2: Lets go into a bit more detail as to why you'd want to use wildcards.

If you declare a method whose signature expect you to pass in List<HasWord>, then the only thing you can pass in is a List<HasWord>.

However, if said signature was List<? extends HasWord> then you could pass in a List<ChildOfHasWord> instead.

Note that there is a subtle difference between List<? extends HasWord> and List<? super HasWord>. As Joshua Bloch put it: PECS = producer-extends, consumer-super.

What this means is that if you are passing in a collection that your method pulls data out from (i.e. the collection is producing elements for your method to use), you should use extends. If you're passing in a collection that your method adds data to (i.e. the collection is consuming elements your method creates), it should use super.

This may sound confusing. However, you can see it in List's sort command (which is just a shortcut to the two-arg version of Collections.sort). Instead of taking a Comparator<T>, it actually takes a Comparator<? super T>. In this case, the Comparator is consuming the elements of the List in order to reorder the List itself.

6
  • 19
    "anything that would work with instanceof" - plus null values. Remember that null values return false for any instanceof check. Jun 9, 2010 at 20:56
  • 12
    Don't forget interfaces. The ? doesn't have to represent a class! Jun 10, 2010 at 15:09
  • 10
    List<HasWord> is exactly what you describe: A list which contains any objects x for which x instanceof HasWord returns true, and null. You don't need a wildcard for this. The wildcard means that it actually can be a list of another type, too, as long as this type is a subtype of HasWord. (Sorry for the late comment.) Aug 12, 2011 at 1:41
  • 1
    PDF link does not seem to be working, but this was useful to me: docs.oracle.com/javase/tutorial/extra/generics/wildcards.html Sep 26, 2012 at 10:09
  • That's weird, I never got a message last year when @user1338062 posted and didn't know the PDF link wasn't working. Fixed now. I also added a link to the book itself.
    – Powerlord
    Apr 19, 2013 at 14:22
84

A question mark is a signifier for 'any type'. ? alone means

Any type extending Object (including Object)

while your example above means

Any type extending or implementing HasWord (including HasWord if HasWord is a non-abstract class)

2
  • 4
    so what does public Set<Class<?>> getClasses() mean? What's the difference with Set<Class>? I'm looking at javax.ws.rs.core.Application.
    – vault
    Apr 23, 2015 at 14:38
  • @vault Set<Class> is a set of raw classes. <?> is implicit Jun 22, 2021 at 20:35
18

List<? extends HasWord> accepts any concrete classes that extends HasWord. If you have the following classes...

public class A extends HasWord { .. }
public class B extends HasWord { .. }
public class C { .. }
public class D extends SomeOtherWord { .. }

... the wordList can ONLY contain a list of either As or Bs or mixture of both because both classes extend the same parent or null (which fails instanceof checks for HasWorld).

3
  • 8
    Not only concrete, also abstract subclasses.
    – bloparod
    Jun 10, 2010 at 2:39
  • 2
    Not only concrete and abstract subclasses, but also sub-interfaces: List<? extends Collection<String>> list = new ArrayList<List<String>>();. Jun 10, 2010 at 15:08
  • 2
    You don't need wildcards for this, actually: List<HasWord> does just as well for this. The point here is that this variable can contain a List<A> or List<B> as well as a List<HasWord>. Aug 12, 2011 at 1:43
16

Perhaps a contrived "real world" example would help.

At my place of work we have rubbish bins that come in different flavours. All bins contain rubbish, but some bins are specialist and do not take all types of rubbish. So we have Bin<CupRubbish> and Bin<RecylcableRubbish>. The type system needs to make sure I can't put my HalfEatenSandwichRubbish into either of these types, but it can go into a general rubbish bin Bin<Rubbish>. If I wanted to talk about a Bin of Rubbish which may be specialised so I can't put in incompatible rubbish, then that would be Bin<? extends Rubbish>.

(Note: ? extends does not mean read-only. For instance, I can with proper precautions take out a piece of rubbish from a bin of unknown speciality and later put it back in a different place.)

Not sure how much that helps. Pointer-to-pointer in presence of polymorphism isn't entirely obvious.

0
8

In English:

It's a List of some type that extends the class HasWord, including HasWord

In general the ? in generics means any class. And the extends SomeClass specifies that that object must extend SomeClass (or be that class).

1
  • 1
    Actually, type instead of class. This works just as well for interfaces. Aug 12, 2011 at 1:44
1

The question mark is used to define wildcards. Checkout the Oracle documentation about them: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.