Command Palette

Search for a command to run...

Phase 8Intermediate9 of 17 in Core Java

Generics

Type-safe reusable code: why generics exist, generic classes and methods, bounds, wildcards and PECS, and what type erasure really does.

Before Java 5, a List could hold anything, so every read needed a cast and every wrong element blew up at run time, often far from the line that caused it. Generics let you write List<String>: a list the compiler knows holds only strings. Mistakes become compile errors, casts disappear, and one class like ArrayList works safely for every element type.

This phase goes from the problem to the full toolset: generic classes and interfaces, generic methods and type inference, bounded type parameters like <T extends Comparable<T>>, wildcards (? extends, ? super) and the PECS rule that tells you which to use, and finally type erasure: what the compiler actually turns your generic code into, and the limits that follow from it (no new T(), no List<int>, no instanceof List<String>).

It builds on Phase 4 (classes, records) and Phase 5 (inheritance and interfaces). Every later phase depends on it: Phase 9's collections are generic types, and Phase 10's lambdas and streams are built on generic functional interfaces like Function<T, R>. Reading signatures such as <T> void sort(List<T> list, Comparator<? super T> c) stops being scary by the end of this phase.

0/6 · 0%
6 topics ~3 h 26 code blocks & diagrams
Start with the first topic
1
8.1

Why Generics Exist

Generics let a class or method work with many types while the compiler still checks which type you used. List<String> is a list the compiler knows holds only strings, so wrong elements become compile errors and the casts disappear.

31 min 4 code practice

2
8.2

Generic Classes and Interfaces

A generic class declares one or more type parameters, like class Box<T>, and uses them as types for its fields, parameters and return values. Each user picks the real type (Box<String>, Box<Integer>), and the compiler checks every use against it.

32 min 4 code practice

3
8.3

Generic Methods

A generic method declares its own type parameters, written before the return type: static <T> T first(List<T> list). The compiler works out T from the arguments and the target at each call, so one method serves every type with full checking.

33 min 4 code practice

4
8.4

Bounded Type Parameters

A bound limits which types a type parameter accepts: <T extends Number> means "any Number or subclass", and <T extends Comparable<T>> means "any type that can compare itself". In return, your generic code may call the bound's methods, like doubleValue() or compareTo(), on values of type T.

34 min 4 code practice

5
8.5

Wildcards and PECS

A wildcard ? stands for "some type I don't name". List<? extends Number> is a list you can safely read Numbers from, and List<? super Integer> is one you can safely add Integers to. PECS, "Producer Extends, Consumer Super", tells you which to use.

34 min 5 code practice

6
8.6

Type Erasure and Its Limits

Java checks generic types at compile time and then erases them: T becomes its bound (usually Object), casts are inserted where values come out, and bridge methods keep overriding working. That's why new T(), new T[n], instanceof List<String> and overloading on List<String> vs List<Integer> are impossible.

36 min 5 code practice