☕Java Generics and Wildcards
Stop sprinkling `?` until the compiler stops yelling, and start designing Java generics on purpose — one PECS rule per drop, ending with a generic utility class whose variance you can defend in code review.
Phase 1Why Generics Exist and What They Cost
See why generics exist and what type erasure costs.
Generics moved type checks from runtime to compile time
6 minGenerics moved type checks from runtime to compile time
At runtime, `List<String>` and `List<Integer>` are the same class
7 minAt runtime, `List<String>` and `List<Integer>` are the same class
`List<String>` is not a `List<Object>` — and that's intentional
6 min`List<String>` is not a `List<Object>` — and that's intentional
A type parameter is one variable you must use consistently
7 minA type parameter is one variable you must use consistently
Phase 2Bounded and Unbounded Wildcards in Practice
Use bounded and unbounded wildcards in real code.
`List<?>` means 'a list of something I will not write into'
6 min`List<?>` means 'a list of something I will not write into'
`? extends T` is the producer — read up to T, do not add
7 min`? extends T` is the producer — read up to T, do not add
`? super T` is the consumer — add T, do not read as T
7 min`? super T` is the consumer — add T, do not read as T
PECS — Producer Extends, Consumer Super, Both means T
8 minPECS — Producer Extends, Consumer Super, Both means T
`<T extends Bound>` is a named upper bound — use it when you need the name
8 min`<T extends Bound>` is a named upper bound — use it when you need the name
Phase 3PECS Across Real Java APIs
Map PECS onto Collections, Comparator, and stream APIs.
Read `Collections.copy` and you've read every PECS signature ever written
7 minRead `Collections.copy` and you've read every PECS signature ever written
`Comparator<? super T>` is why a `Comparator<Number>` can sort `List<Integer>`
7 min`Comparator<? super T>` is why a `Comparator<Number>` can sort `List<Integer>`
`Function<? super T, ? extends R>` is PECS twice in one signature
7 min`Function<? super T, ? extends R>` is PECS twice in one signature
Apply PECS to a real review and pick the right wildcard in 30 seconds
8 minApply PECS to a real review and pick the right wildcard in 30 seconds
Phase 4Design a Generic Utility Class
Design a generic utility class with correct variance.
Design a `Pipeline<T>` utility class with correct variance throughout
20 minDesign a `Pipeline<T>` utility class with correct variance throughout
Frequently asked questions
- What does PECS stand for in Java generics?
- This is covered in the “Java Generics and Wildcards” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- When do I use `? extends T` versus `? super T`?
- This is covered in the “Java Generics and Wildcards” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Why can't I add elements to a `List<? extends Number>`?
- This is covered in the “Java Generics and Wildcards” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- What is type erasure and why does it matter?
- This is covered in the “Java Generics and Wildcards” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Are Java generics covariant or invariant?
- This is covered in the “Java Generics and Wildcards” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Related paths
🐍Python Decorators Introduction
Build one mental model for Python decorators that covers closures, argument passing, functools.wraps, and stacking — then ship a working caching or logging decorator from scratch in under 30 lines.
🦀Rust Lifetimes Explained
Stop reading `'a` as line noise and start reading it as scope arithmetic — one failing snippet at a time — until you can thread lifetimes through a small parser or iterator adapter without fighting the borrow checker.
☸️Kubernetes Core Concepts
Stop drowning in 30+ resource types. Build the mental model one primitive at a time -- pods, deployments, services, ingress, config -- then deploy a real app with rolling updates and health checks.
📈Big O Intuition
Stop treating Big O as math you memorized for an interview — build the intuition to spot O(n²) disasters, pick the right data structure without thinking, and rewrite a slow function from O(n²) to O(n) in under five minutes.