Card 09/ 09
Recap: Generics
Earlier errors, erasure, invariance, wildcards, and what erasure forbids, with where each was.
Generics come down to the five ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| A type parameter moves a failure from the reading line to the writing line | Generics: Moving a Type Error From Run Time to Compile Time |
Type arguments are erased, so List<String> is a List at run time | Type Erasure: What Is Left of List<String> at Run Time |
A List<String> is not a List<Object>, because adding would corrupt it | Why a List<String> Is Not a List<Object> |
Producer extends, consumer super: the wildcard states the direction | Bounded Wildcards: ? extends and ? super |
new T[], new T(), instanceof T and T.class are all refused | Erasure Limits: Why You Cannot Write new T[] |
Every awkward rule here is erasure: the compiler knows the type argument and the runtime does not, so what can be checked early is, and what needs the type while running is refused.
← → move through sections and cards