Java generics naming conventions

According to the Java documentation :

The most commonly used type parameter names are:

E - Element (widely used Java Collections Framework)

K - Key

N - Number

T - Type

V - Value

S, U, V, etc. - 2nd, 3rd, 4th types

However, I saw codes like this

public <R> Observable<R> compose(Transformer<? super T, ? extends R> transformer) { return ((Transformer<T, R>) transformer).call(this); } 

It uses R, from what source can I find out what R means here?

+7
java generics
source share
2 answers

Result or Return Type. The first is especially common in JDK 8 classes.

Consider JavaDocs for java.util.function.Function , for example:

R - type of function result

The <R> and , ?R> templates do not appear in OpenJDK 7 classes (link: I downloaded the source and made grep), but they do appear in several classes to create the Java toolchain itself, such as javac. There R sometimes seems to mean "result" and sometimes for the return type.

+8
source share

It is very simple. Here, R stands for “return type”.

If you look at the “adapter design pattern”, which converts one type to another type to work with two completely different interfaces. There you can find such general meanings.

If you write this method that converts or calls an object of type T and returns an observable object of another type, what will you use for the return type? There is no hard and fast rule for generic type names. The only rule here is that it should be concise and meaningful.

+1
source share

All Articles