WenChao
WenChao

Reputation: 3665

Java generics naming conventions

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?

Upvotes: 7

Views: 661

Answers (2)

Braj
Braj

Reputation: 46871

It is very simple. Here R stands for "return type".

If you look at "adaptor design pattern" that convert one type into another type to work with two completely different interfaces. There you can find such generic values.

If you are writing this method that transforms or calls on object of type T and returns observable object of another type then what will you use for type of returned value? There is no hard and fast rule for generic type naming. The only rule applies here is that it should be short and meaningful.

Upvotes: 1

yshavit
yshavit

Reputation: 43401

"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."

Upvotes: 8

Related Questions