Glossary · Java

Generics

Also known as: java generics, type parameters, type erasure, wildcards

A Java feature that lets classes and methods take type parameters, such as List<String>, so the compiler catches type errors and you avoid casts.

Example

// Generic method: works for any Comparable type
static <T extends Comparable<T>> T max(List<T> items) {
    T best = items.getFirst();
    for (T item : items) {
        if (item.compareTo(best) > 0) best = item;
    }
    return best;
}

Integer top = max(List.of(3, 9, 4));   // no cast needed

PECS: wildcards in one line

Producer extends, Consumer super:

void copy(List<? extends Number> src, List<? super Number> dst) { dst.addAll(src); }

Type erasure

Type parameters exist only at compile time. At runtime List<String> and List<Integer> are both just List, so you cannot write new T() or instanceof List<String>.

See also

Used in these posts

Java • 78 min read Reader level: Advanced

What you'll learn

  • The real memory and CPU-cache costs of Java collections
  • What type erasure means for generics in practice
  • How lambdas work under the hood with invokedynamic, and what streams cost
  • How to prompt AI assistants and verify their performance claims
Mastering Core Java & Modern Features: Beat AI by Knowing the Cost

Understand the internal mechanics, memory footprints, and CPU cache costs of Collections, Generics, Lambdas, and Streams to write high-performance Java code that outpaces AI assistants.

#java#jvm#performance#collections
Java • 4 min read Reader level: Advanced

What you'll learn

  • Why generics exist and how the type hierarchy works
  • How to use upper and lower bounded wildcards
  • How to apply the PECS principle when designing APIs
  • What type erasure means for your code at runtime
Java Generics Deep Dive: Wildcards, Bounds, and Type Erasure

A comprehensive guide to Java Generics — covering bounded wildcards, PECS principle, type erasure gotchas, and practical patterns for writing reusable, type-safe APIs.

#java#generics#type-safety#jvm