0

I use javap often.

So when I see JDK classes across, I understood that probably:

  • T means Type;
  • E means Enumertion;
  • Meaning of V is unclear for me.

Please shed some light on this question?

Maybe I am wrong. But I see several JDK classes belongs to package java.util.function.* , java.util.concurrent.* has this V, T, E in place of class type, return type.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Dev Anand Sadasivam
  • 699
  • 7
  • 21
  • 49

2 Answers2

2

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

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
1

Java has a feature called generics which is used to develop classes such that its functionality can be used with any type of objects. The letter you see like T, E or V are just place holders. For example List<E>. This is substituted with actual class when start using the list like below.

List<Integer> numbers = new ArrayList<Integer>();

Java uses this approach to check type safety so that a String object cannot be added to an Integer list.

numbers.add(1); //Good
numbers.add("Harry"); //Compilation error

Here are some useful resources.

danronmoon
  • 3,814
  • 5
  • 34
  • 56