Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>todd.run is totally right on, but that's only half the answer. There are also use cases for choosing <code>&lt;T&gt;</code> over <code>&lt;?&gt;</code> (or vice versa) that apply when you don't add type parameter to the class that encloses the method. For example, consider the difference between </p> <pre><code>public &lt;E extends JLabel&gt; boolean add(List&lt;E&gt; j) { boolean t = true; for (JLabel b : j) { if (b instanceof JLabel) { t = t &amp;&amp; labels.add(b); } } return t; } </code></pre> <p>and </p> <pre><code>public boolean add(List&lt;? extends JLabel&gt; j) { boolean t = true; for (JLabel b : j) { if (b instanceof JLabel) { t = t &amp;&amp; labels.add(b); } } return t; } </code></pre> <p>The first method will actually not compile UNLESS you add an appropriate type parameter to the enclosing class, whereas the second method WILL compile regardless of whether the enclosing class has a type parameter. If you do not use <code>&lt;?&gt;</code>, then you are locally responsible for telling the compiler how to acquire the type that will be filled in by the letter used in its place. You frequently encounter this problem - needing to use ? rather than T - when attempting to write generic methods that use or need "extends" and "super." A better but more elaborate treatment of this issue is on page 18 of <a href="http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf" rel="nofollow noreferrer">Gilad Bracha's Generics Tutorial (PDF)</a>. Also see <a href="https://stackoverflow.com/questions/695298/java-field-type-for-a-value-of-a-generically-recusive-self-type">this stack overflow question</a> whose answer illuminates these issues.</p> <p>Check out this stack overflow link for information about your second question: <a href="https://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens">Java generics - type erasure - when and what happens</a>. While I don't know the answer to your question about the compile time difference between <code>&lt;?&gt;</code> and <code>&lt;T&gt;</code>, I'm pretty sure the answer can be found at <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ001" rel="nofollow noreferrer">this FAQ</a> that erickson mentioned in that post.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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