According to the Java documentation:
The most commonly used type parameter names are:
E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
However , I have seen codes like this
public <R> Observable<R> compose(Transformer<? super T, ? extends R> transformer) {
return ((Transformer<T, R>) transformer).call(this);
}
Here it is using R, from what source can I find out what R means in here?
Also, K is often used for a generic key type and V for a value type associated with it; in some cases E is used for the type of "elements" in a collection. Just to add - it's rare but occasionally you can use something more than letters for generic types.
Generics were added to Java to ensure type safety. And to ensure that generics won't cause overhead at runtime, the compiler applies a process called type erasure on generics at compile time. Type erasure removes all type parameters and replaces them with their bounds or with Object if the type parameter is unbounded.
A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
The polymorphism applies only to the 'base' type (type of the collection class) and NOT to the generics type.
"Result" or "return type." The former is particularly common in the JDK 8 classes.
Consider the JavaDocs for java.util.function.Function, for instance:
R - the type of the result of the function
The patterns <R>
and , ?R>
don't appear in OpenJDK 7's classes (citation: I downloaded the source and did a grep), but they do appear in several classes for building the Java toolchain itself, like javac. There, the R sometimes seems to stand for "result" and sometimes for "return type."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With