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).
Advertisements. The question mark (?), represents the wildcard, stands for unknown type in generics. There may be times when any object can be used when a method can be implemented using functionality provided in the Object class or When the code is independent of the type parameter.
A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
Multiple parametersYou can also use more than one type parameter in generics in Java, you just need to pass specify another type parameter in the angle brackets separated by comma.
? 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.
A question mark is a signifier for 'any type'. ?
alone means
Any type extending
Object
(includingObject
)
while your example above means
Any type extending or implementing
HasWord
(includingHasWord
ifHasWord
is a non-abstract class)
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
).
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.
In English:
It's a
List
of some type that extends the classHasWord
, includingHasWord
In general the ?
in generics means any class. And the extends SomeClass
specifies that that object must extend SomeClass
(or be that class).
The question mark is used to define wildcards. Checkout the Oracle documentation about them: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With