Sub topic 3
Naming Type Parameters: T, E, K, V
In the last chapter you wrote Box<T>. The T there is a type parameter. A type parameter is a placeholder name for a type that the caller fills in later. Java lets you choose any valid name for it. Most Java code still uses a single capital letter, and each letter usually carries a meaning.
Why Use Single Letters?
A type parameter is different from a real class. A short capital letter makes that clear. When you see String value, you know String is a real class. When you see T value, you know T is a placeholder.
The letters also tell you what role the type plays. Code is easier to read when everyone follows the same habits.
The Common Letters
| Letter | Stands for | Typical use | Example in the JDK |
|---|---|---|---|
| `T` | Type | Any general type | `Optional<T>` |
| `E` | Element | An item stored in a collection | `List<E>`, `Set<E>` |
| `K` | Key | The key in a map | `Map<K, V>` |
| `V` | Value | The value in a map | `Map<K, V>` |
| `N` | Number | A numeric type | Common in tutorials |
| `R` | Result | What a function returns | `Function<T, R>` |
| `S`, `U` | 2nd, 3rd type | Extra types after `T` | `BiFunction<T, U, R>` |
The JDK is the Java Development Kit. Its own classes follow these habits, so learning the letters helps you read its documentation.
A Worked Example
This program uses E for the items in a stack and K and V for a simple key and value holder. A stack is a list where you add and remove items only at the end.
import java.util.ArrayList;
import java.util.List;
public class NamingDemo {
// E: the element type stored in the stack
static class Stack<E> {
private final List<E> items = new ArrayList<>();
void push(E item) {
items.add(item);
}
E pop() {
return items.remove(items.size() - 1);
}
}
// K and V: a key and the value linked to it
static class Entry<K, V> {
final K key;
final V value;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
Stack<String> words = new Stack<>();
words.push("first");
words.push("second");
System.out.println(words.pop()); // second
Entry<String, Integer> score = new Entry<>("Ada", 95);
System.out.println(score.key + " = " + score.value); // Ada = 95
}
}Save it as NamingDemo.java, then run javac NamingDemo.java and java NamingDemo.
Look at Stack<E>. The letter E tells the reader that this type describes the stack's items. With Entry<K, V>, you can tell which type is the key and which is the value just from the declaration.
Longer Names
Sometimes one letter is not clear enough, for example when a class has many type parameters. Some teams then use short names that end in T, such as RequestT and ResponseT. These are allowed. Use them only when a single letter would confuse readers.
Exercise
You are writing a generic class that holds a cache, where each item is looked up by a key. Which letters would you use for its two type parameters, and in what order?
Show answer
Use K for the key type and V for the value type, declared as Cache<K, V>. This matches Map<K, V> in the JDK, so readers recognize it right away.
Exercise
What is wrong with declaring class Holder<Integer> { Integer item; }?
Show answer
The name Integer becomes a type parameter that hides the real java.lang.Integer class. Inside Holder, Integer can mean any type, which misleads readers. Rename it to T.