Java Generics (Wildcards)

Pablo Fernandez picture Pablo Fernandez · Oct 30, 2008 · Viewed 70.1k times · Source

I have a couple of questions about generic wildcards in Java:

  1. What is the difference between List<? extends T> and List<? super T>?

  2. What is a bounded wildcard and what is an unbounded wildcard?

Answer

Bill the Lizard picture Bill the Lizard · Oct 31, 2008

In your first question, <? extends T> and <? super T> are examples of bounded wildcards. An unbounded wildcard looks like <?>, and basically means <? extends Object>. It loosely means the generic can be any type. A bounded wildcard (<? extends T> or <? super T>) places a restriction on the type by saying that it either has to extend a specific type (<? extends T> is known as an upper bound), or has to be an ancestor of a specific type (<? super T> is known as a lower bound).

The Java Tutorials have some pretty good explanations of generics in the articles Wildcards and More Fun with Wildcards.