In Java 5 Generic types were introduced in the language. In a nutshell generics - is a mechanism of type safety, they are meant to make the code more robust. Generics allow writing clean and highly reusable code without performing instanceof
checks and type-casting (to be precise type-casts are still there, they would be added by the compiler under the hood, and they are safe).
V
, T
, E
, etc. are so-called type variables. These are only placeholders, not the actual types, i.e. it's a way to tell the compiler that you expect a particular type at runtime and you can refer to this type using type variable.
In your code, you can call type variables with any name you like. The common convention is to use single uppercase letters like T
, R
, but compiler be happy with Cat
as a type variable, and even with a name of existing JDK classes and interfaces like Integer
(which is highly discouraged, but your code would compile).
public class MyClass<T> {} // OK
public class MyClass<CAT> {} // OK
public class MyClass<Integer> {} // OK, note `Integer` is just a placeholder and has the the same meaning as `T` or `A`
public class MyClass<super> {} // will NOT compile, keywords and restricted identifiers (like `record` which isn't a keyword) can't be used as generic type parameters
Regarding type variables that you can encounter in the JDK, there's no common policy that has been listed anywhere. Sometimes there's some informal intuition behind these names, sometimes probably not (e.g. when you see T
, U
, V
defined together as generic variables you can clearly observe that it's an order of letters in the English alphabet, T
- stands for type, and U
, V
are simply some other types distinct from T
and each other).
Speaking of cases when probably there's some meaning:
E
- in Collection<E>
most likely means element, and in Enum<E>
- stands for enum.
R
in Function<T,R>
stands for result, or return type of the function.
K
and V
in Map<K,V>
and Map.Entry<K,V>
stand for key and value.
A
in the Collector<T,A,R>
interface is meant to represent accumulation type (type of the mutable container which collector uses internally).