← Back to Java

Java Generics

JavaMidjava

The Question

What are generics in Java?

What a Strong Answer Covers

  • type parameters for classes/methods/interfaces
  • compile-time type safety
  • no explicit casting
  • bounded types extends
  • wildcards upper/lower bounded
  • Box<T> example

Senior-Level Answer

Java generics, introduced in Java 5, allow classes, interfaces, and methods to be parameterized by type. Instead of writing a container that holds Object and requires manual casting, you declare List<String>, and the compiler enforces that only String values are added and returns String values without explicit casts. This moves type errors from runtime ClassCastException to compile-time errors, significantly improving code safety and clarity.

Generic classes are declared with a type parameter: class Box<T> { private T value; }. Generic methods declare their type parameter before the return type: public <T> T identity(T value). Type parameters are conventionally named T (type), E (element), K (key), V (value), R (return type).

Bounded type parameters constrain the valid type arguments. <T extends Comparable<T>> requires T to implement Comparable, enabling comparisons within the generic method. <T extends Number> restricts T to Number subclasses.

Wildcards add flexibility when consuming generic types without writing generic methods. List<?> accepts a list of any type (useful when only reading, via the upper-bounded principle). List<? extends Number> accepts List<Integer>, List<Double> etc. — you can read from it safely but not write (since the concrete type is unknown). List<? super Integer> accepts List<Integer>, List<Number>, List<Object> — you can write Integer into it but reading yields only Object.

The PECS mnemonic (Producer Extends, Consumer Super) governs wildcard selection: use ? extends T when the list produces values you read; use ? super T when the list consumes values you write.

Generics are a compile-time mechanism only. The type parameters are erased from bytecode at runtime (type erasure) — List<String> and List<Integer> are both just List at the JVM level. This is why you cannot write new T() or new T[] directly, perform instanceof checks like obj instanceof List<String>, or overload methods that differ only in generic parameter.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains compile-time type safety, basic generic class/method syntax, and upper-bounded wildcards with extends.

3/3 — Strong Answer

Covers PECS rule with accurate explanation, type erasure and its practical constraints, bounded type parameters, and the difference between wildcards in usage position vs. type parameter declarations.

Common Mistakes

  • Confusing <T extends Number> (declaration) with <? extends Number> (wildcard in use) — they serve different purposes and appear in different contexts.
  • Not knowing why you can't write new T() — type erasure means T is unknown at runtime, so the JVM can't call the right constructor.
  • Misapplying PECS — trying to write to a List<? extends Number> and getting a compile error without understanding why.

Follow-Up Questions

  • Why can't you add elements to a List<? extends Number>? — The exact type is unknown — it could be List<Integer>, List<Double>, or List<Float>. Adding an Integer to a List<Double> would be unsafe, so the compiler prohibits all writes except null.
  • What is the PECS principle and give a concrete example? — Producer Extends Consumer Super: Collections.copy(List<? super T> dest, List<? extends T> src) — src produces values (extends), dest consumes values (super).
  • Why can't you create generic arrays like new T[10]? — Arrays are covariant and enforce type at runtime, but T is erased. The JVM would create Object[10] and couldn't enforce the element type, defeating the purpose and risking heap pollution.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout