Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I explain in <a href="https://stackoverflow.com/questions/2248390/java-generics-collections-max-signature-and-comparator/2248503#2248503">my answer</a> to another question, PECS is a mnemonic device created by Josh Bloch to help remember <strong>P</strong>roducer <code><strong>e</strong>xtends</code>, <strong>C</strong>onsumer <code><strong>s</strong>uper</code>.</p> <blockquote> <p>This means that when a parameterized type being passed to a method will <em>produce</em> instances of <code>T</code> (they will be retrieved from it in some way), <code>? extends T</code> should be used, since any instance of a subclass of <code>T</code> is also a <code>T</code>.</p> <p>When a parameterized type being passed to a method will <em>consume</em> instances of <code>T</code> (they will be passed to it to do something), <code>? super T</code> should be used because an instance of <code>T</code> can legally be passed to any method that accepts some supertype of <code>T</code>. A <code>Comparator&lt;Number&gt;</code> could be used on a <code>Collection&lt;Integer&gt;</code>, for example. <code>? extends T</code> would not work, because a <code>Comparator&lt;Integer&gt;</code> could not operate on a <code>Collection&lt;Number&gt;</code>.</p> </blockquote> <p>Note that generally you should only be using <code>? extends T</code> and <code>? super T</code> for the parameters of some method. Methods should just use <code>T</code> as the type parameter on a generic return type.</p>
 

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