What does the question mark in Java generics' type parameter mean?
? 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.
Significance of ? in generics
It is a wildcard which means any type that is extending Object(which also includes object).
So you can say that <?>
is a shorthand for <? extends Object>
Check Oracle docs for Type Arguments and Wildcards
From here:
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.
Parameter with question mark and super
?
here means everything that is a superclass of T
super
means what you can put into the class (at most this, perhaps a superclass).
Because super
indicates the lower bounding class of a generic element. So, Action1<? super T>
could represent Action1<T>
or Action1<Object>
.
What does the symbol ? mean?
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.
So in order to answer the question: it is a Wildcard-> Official Doc so you can handle classes event when you dont know the type.
What does the question mark in java mean?
In Java ?
is known as Wildcard, you can use it to respresent an unknown type
.
The upper bounded wildcard, , where Foo is any type, matches Foo and any subtype of Foo. The process method can access the list elements as type Foo:
public static void process(Map<? extends A> list) {
/* code */
}
In your case it is known as Upper bounded wildcard.
http://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html
putAll(Map<? extends K, ? extends V> map)
It means, that any object, that can extend the A
class is applicable in this conditionn.
In Java Collections Map Key,? What does ? refer to?
The question mark (?) represents an unknown type.
In your example, Map<Key, ?>
, it means that it will match a map containing values of any type. It does not mean you can create a Map<Key, ?>
and insert values of any type in it.
Quoting from the 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.
For example, say you want to create a function that will print the values of any map, regardless of the value types:
static void printMapValues(Map<String, ?> myMap) {
for (Object value : myMap.values()) {
System.out.print(value + " ");
}
}
Then call this function passing a Map<String, Integer>
as argument:
Map<String, Integer> myIntMap = new HashMap<>();
myIntMap.put("a", 1);
myIntMap.put("b", 2);
printMapValues(myIntMap);
And you would get:
1 2
The wildcard allows you to call the same function passing a Map<String, String>
, or any other value type, as argument:
Map<String, String> myStrMap = new HashMap<>();
myStrMap.put("a", "one");
myStrMap.put("b", "two");
printMapValues(myStrMap);
Result:
one two
This wildcard is called unbounded, since it gives no information about the type. There are a couple of scenarios where you may want to use the unbounded wildcard:
- If you're calling no methods except those defined in the
Object
class. - When you're using methods that don't depend on the the type parameter, such as
Map.size()
orList.clear()
.
A wildcard can be unbounded, upper bounded, or lower bounded:
List<?>
is an example of an unbounded wildcard. It represents a list of elements of unknown type.List<? extends Number>
is an example of an upper bounded wildcard. It matches aList
of typeNumber
, as well as its subtypes, such asInteger
orDouble
.List<? super Integer>
is an example of a lower bounded wildcard. It matches aList
of typeInteger
, as well as its supertypes,Number
andObject
.
Related Topics
What Is the Use of Interface Constants
Convert Integer into Byte Array (Java)
Modify Request Parameter with Servlet Filter
What Does Java Option -Xmx Stand For
How to Implement a Map with Multiple Keys
What Is the Best Approach for Using an Enum as a Singleton in Java
Returning Null as an Int Permitted with Ternary Operator But Not If Statement
Convert Java.Util.Date to What "Java.Time" Type
Java.Lang.Unsatisfiedlinkerror No *****.Dll in Java.Library.Path
Why Java Classes Do Not Inherit Annotations from Implemented Interfaces
Getting Rsa Private Key from Pem Base64 Encoded Private Key File