Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Generics — Wildcards, Bounded Types, PECS

Compare a bounded type parameter <T extends Number> with a wildcard <? extends Number>. When do you need each?

Level III · MidGenerics — Wildcards, Bounded Types, PECS
Short answer

Use a named type parameter <T extends Number> when you need to refer to the type elsewhere in the signature — relating arguments to each other or to the return type (e.g., <T extends Comparable<T>> T max(List<T>)).

Explanation

Use a wildcard when the method doesn't need to name the type and just operates on 'some unknown subtype', which keeps the signature simpler. A rule of thumb: if a type variable appears only once and you don't need its name, a wildcard is the cleaner choice.

Common mistakes
  • Using a wildcard when you need the type in two places (bounded type parameter).
  • Mixing up when a method needs <T> versus just <?>.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Generics — Wildcards, Bounded Types, PECS.

Practise free

More on Generics — Wildcards, Bounded Types, PECS