What does the question mark in Java generics' type parameter mean?
The ?
in Java generics is a wildcard symbol that means "unknown type". It's used liberally when you don’t need to strictly define type parameter bounds. For more controlled scenarios, ? extends T
is employed where T
or its descendant types are desired. You get read-access to them but can't add new ones. On the other hand, ? super T
allows you to insert instances of T
or its ancestors into a collection. This liberates your code from rigid type check and fosters greater flexibility. Here's a handy code sample reflecting bounded wildcard usage:
Tuple of Wildcards: Commonly Misunderstood Concepts
Swashbuckling with ? extends T
The wildcard ? extends T
is your friend when you are dealing with classes that are subtypes/twig of T
. A Rand McNally direction when you want to access methods defined in the superclass T
or subclasses thereof. Applying this wildcard unlocks a flexible gateway welcoming various types. Who wouldn't enjoy such open-mindedness, amirite? 😏
Parties with ? super T
? super T
is the life of the party when you want to insert instances of T
or its subclasses into a collection. It's a champion of the PECS (Producer Extends, Consumer Super) acronym, ensuring your method can deliver gifts to collections expecting T
or a more general type. Gift-giving was never this generous and unrestricted since Santa's been on a diet 🎅:
Bounded Wildcards: Jack of all Trades
Using ? extends T
and ? super T
broadcasts intentions crystal clear than raw types or unbounded wildcards. It's a return ticket to better design and safer code. For instance, sorting algorithms can inhale a Comparator<? super T>
:
It's clear the comparator will choose a dance partner from SomeClass
or its superclasses.
Life Lessons from Type Safety
- Use
? extends T
to keep things read-safe, no adding randoms to the party. - Prefer
? super T
to insert elements while maintaining the original type's dignity. - With bounded wildcards, you can ditch the
instanceof
checks; polymorphism is the new black.
A Day in the Life of Generics
Imagine an API builder for different data types. Bounded wildcards shape up versatile methods:
Your Collection
is now a hotbed for any subtype of E
. Winning!
Was this article helpful?