what does List<? extends SomeClass> mean?
Solution 1
This is a generic declaration.
It is used to check types at compile time. You could put any object into a list, but that would make it more difficult to maintain and could cause ClassCastException
s, if not used appropriate.
<? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder>
means "allow every class which extends DestInfoOrBuilder
".
Solution 2
In java generics programing,there are two kinds of bounds when using wildcards.
1)Upper Bounded Wildcards .
like: ArrayList <? extends Number> p
,it means you can use anything extends Number to fill the ArrayList.
2)Lower Bounded Wildcards.
like:ArrayList<? super Integer> list
,
it means you have to pass anything who is the super class (such as Number,Object)of Integer to fill the ArrayList.
for more information,refer wildcards.
Solution 3
In java 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 more information read Java Generic's Wildcards and Generics: The wildcard operator
Comments
-
Sayakiss almost 2 years
I saw that definition in a protobuf generated java file:
java.util.List<? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder> foo();
But what dose
<?
andextends
mean? I can understandList<SomeClass>
I can't understandList<? extends SomeClass>
..