WILLIAM TECH LABLEARN · BUILD · DEPLOY
Java

Java Generics: understanding wildcards without guessing

Generics help make APIs safer. The important distinction between extends and super is about where values flow.

← Blog

Generics help make APIs safer. The important distinction between extends and super is about where values flow.

Why wildcards matter

Use <? extends T> when reading values as T from a producer, and <? super T> when writing T values into a consumer.

A simple example

A List<? extends Number> is safe to read as Number, but you cannot simply add an Integer to it. A List<? super Integer> accepts Integer values, but reading a value yields Object.

Takeaway

The PECS rule — Producer Extends, Consumer Super — is a useful guideline, not a replacement for clear API design.