Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It depends on what you <em>need</em> to do. You need to use the bounded type parameter if you wanted to do something like this:</p> <pre><code>public &lt;T extends Shape&gt; void addIfPretty(List&lt;T&gt; shapes, T shape) { if (shape.isPretty()) { shapes.add(shape); } } </code></pre> <p>Here we have a <code>List&lt;T&gt; shapes</code> and a <code>T shape</code>, therefore we can safely <code>shapes.add(shape)</code>. If it was declared <code>List&lt;? extends Shape&gt;</code>, you can <em>NOT</em> safely <code>add</code> to it (because you may have a <code>List&lt;Square&gt;</code> and a <code>Circle</code>).</p> <p>So by giving a name to a bounded type parameter, we have the option to use it elsewhere in our generic method. This information is not always required, of course, so if you don't need to know that much about the type (e.g. your <code>drawAll</code>), then just wildcard is sufficient.</p> <p>Even if you're not referring to the bounded type parameter again, a bounded type parameter is still required if you have multiple bounds. Here's a quote from <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ203" rel="noreferrer">Angelika Langer's Java Generics FAQs</a></p> <blockquote> <p><strong>What is the difference between a wildcard bound and a type parameter bound?</strong></p> <p>A wildcard can have only one bound, while a type parameter can have several bounds. A wildcard can have a lower or an upper bound, while there is no such thing as a lower bound for a type parameter. </p> <p>Wildcard bounds and type parameter bounds are often confused, because they are both called bounds and have in part similar syntax. […]</p> <p><strong>Syntax</strong>:</p> <pre><code> type parameter bound T extends Class &amp; Interface1 &amp; … &amp; InterfaceN wildcard bound upper bound ? extends SuperType lower bound ? super SubType </code></pre> <p>A wildcard can have only one bound, either a lower or an upper bound. A list of wildcard bounds is not permitted. </p> <p>A type parameter, in constrast, can have several bounds, but there is no such thing as a lower bound for a type parameter. </p> </blockquote> <p>Quotes from <em>Effective Java 2nd Edition, Item 28: Use bounded wildcards to increase API flexibility</em>:</p> <blockquote> <p>For maximum flexibility, use wildcard types on input parameters that represent producers or consumers. […] PECS stands for producer-<code>extends</code>, consumer-<code>super</code> […]</p> <p><strong>Do not use wildcard types as return types</strong>. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. <strong>If the user of the class has to think about wildcard types, there is probably something wrong with the class's API</strong>.</p> </blockquote> <p>Applying the PECS principle, we can now go back to our <code>addIfPretty</code> example and make it more flexible by writing the following:</p> <pre><code>public &lt;T extends Shape&gt; void addIfPretty(List&lt;? super T&gt; list, T shape) { … } </code></pre> <p>Now we can <code>addIfPretty</code>, say, a <code>Circle</code>, to a <code>List&lt;Object&gt;</code>. This is obviously typesafe, and yet our original declaration was not flexible enough to allow it.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2723397/java-generics-what-is-pecs">Java Generics: What is PECS?</a></li> <li><a href="https://stackoverflow.com/questions/2310449/can-someone-explain-what-does-super-t-mean-and-when-should-it-be-used-and-how">Can someone explain what does <code>&lt;? super T&gt;</code> mean and when should it be used and how this construction should cooperate with <code>&lt;T&gt;</code> and <code>&lt;? extends T&gt;</code>?</a></li> </ul> <hr> <h3>Summary</h3> <ul> <li>Do use bounded type parameters/wildcards, they increase flexibility of your API</li> <li>If the type requires several parameters, you have no choice but to use bounded type parameter</li> <li>if the type requires a lowerbound, you have no choice but to use bounded wildcard</li> <li>"Producers" have upperbounds, "consumers" have lowerbounds</li> <li>Do not use wildcard in return types</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload