What are explicit type parameters in Java Generics?
Answer:
We could have written the signature for this method another way, without using wildcards at all:
class Collections {
public static <T, S extends T>
void copy(List<T> dest, List<S> src) {
...
}
This is fine, but while the first type parameter is used both in the type of dst and in the bound of the second type parameter,
S, S itself is only used once, in the type of src, nothing else depends on it. This is a sign that we can replace S with a wildcard. Using wildcards is clearer and more concise than declaring explicit type parameters, and should therefore be preferred whenever possible. Wildcards also have the advantage that they can be used outside of method signatures, as the types of fields, local variables and arrays. Here is an example.