Question

I saw that definition in a protobuf generated java file:

java.util.List<? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder> foo();

But what dose <? and extends mean? I can understand List<SomeClass> I can't understand List<? extends SomeClass>..

Was it helpful?

Solution 2

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 ClassCastExceptions, if not used appropriate.

<? extends xxx.yyy.zzz.proto.BasicMessage.DestInfoOrBuilder> means "allow every class which extends DestInfoOrBuilder".

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top