World 14 of 26 · Intermediate

🧪 Generics

12 Java concepts, each explained step by step with real code, a common trap and practice questions.

  1. Why genericsCompile-time type safety, no casts, no ClassCastException at runtime.
  2. Generic classes & interfacesclass Box<T>, type parameter naming (T, E, K, V).
  3. Generic methods<T> T first(List<T> list) and type inference.
  4. Diamond operatornew ArrayList<>() infers type arguments.
  5. Bounded type parameters<T extends Number>, multiple bounds with &.
  6. Generics are invariantList<Integer> is not a List<Number>, unlike arrays.
  7. Wildcards?, ? extends T (read), ? super T (write).
  8. PECSProducer Extends, Consumer Super.
  9. Type erasureGeneric types are erased at runtime; bridge methods.
  10. Generic restrictionsNo primitives, no new T(), no T[] creation, no instanceof List<String>, no static T fields.
  11. Raw typesLegacy compatibility, unchecked warnings, heap pollution.
  12. Recursive bounds<T extends Comparable<? super T>> as used by Collections.max.