简体   繁体   中英

What is difference between <? extends Object> and <E extends Object>?

What is difference between <? extends Object> <? extends Object> and <E extends Object> ? When should one be used over the other?

Here are some differences that immediately comes to my mind:

  • Type parameter bounds can specify multiple bounds - T extends A & B , but with wildcard you cannot specify multiple bounds - ? extends A & B ? extends A & B is invalid.

  • You can have lower bounds with wildcard - ? super A ? super A is valid, but not with the type parameter - T super A is not valid.

  • You cannot use wildcard bounds while creating a generic type. You have to use type parameter bounds.

  • Inside a method, if you want some relation between the type parameters of arguments passed, then you have to use type parameter bounds. For eg, you want to pass two parameterized type with same type parameter. You can't do this with wildcard bounds. So the following method declaration will take two list of same type parameter, that extends Number .

     public <T extends Number> void merge(List<T> list1, List<T> list2) { } 

To end with, I'll add some points from Effective Java - Item 28: Use bounded wildcards to increase API flexibility :

For maximum flexibility, use wildcard types on input parameters that represent producers or consumers. If an input parameter is both a producer and a consumer, then wildcard types will do you no good: you need an exact type match, which is what you get without any wildcards.

Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. If the user of a class has to think about wildcard types, there is probably something wrong with the class's API.


References:

<? extends Object> <? extends Object> is a bounded wildcard (an unknown that extends Object , whereas <E extends Object> is type bounded ( E requires a Parameterized type that extends Object ).

Most subtle differences of bounded wildcards from the parameterized upper bound:

  • Bounded wildcards cannot be used when creating Generic type classes, only typed parameters are allowed.

  • Bounded wildcards cannot be used in Collection s where it requires to add items in the a collection, as it is an unknown type, and the collection doesn't know what type it is receiving, hence it is not type safe.

  • Bounded wildcards cannot be bounded to multiple generic types (bounds).

I hope this helps.

Use E if you need to refer to the type later on. Use ? if you never have to refer to the type again.

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 more info check this

<E> declares a type argument.
You can only use it when creating a generic type or method.

<? ...> <? ...> is a wildcard value of a type argument.
You can only use it when creating a closed generic type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM