Generics in Java — a practical guide to Java generics with clear examples you can reuse in real projects.
Java Tutorial Series (58/90). Prefer one article? Read the complete Java tutorial.
Short description
Generics catch type errors at compile time and remove most casting.
Generic box
class Box<T> {
private T value;
void set(T value) { this.value = value; }
T get() { return value; }
}
Box<String> box = new Box<>();
box.set("Java");