Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The big difference between</p> <pre><code>public &lt;T extends Animal&gt; void takeThing(ArrayList&lt;T&gt; list) </code></pre> <p>and</p> <pre><code>public void takeThing(ArrayList&lt;? extends Animal&gt; list) </code></pre> <p>is that in the former method you can refer to "T" within the method as the concrete class that was given. In the second method you cannot do this.</p> <p>Here a more complex example to illustrate this:</p> <pre><code>// here i can return the concrete type that was passed in public &lt;T extends Animal&gt; Map&lt;T, String&gt; getNamesMap(ArrayList&lt;T&gt; list) { Map&lt;T, String&gt; names = new HashMap&lt;T, String&gt;(); for (T animal : list) { names.put(animal, animal.getName()); // i assume there is a getName method } return names; } // here i have to use general Animal public Map&lt;Animal, String&gt; getNamesMap(ArrayList&lt;? extends Animal&gt; list) { Map&lt;Animal, String&gt; names = new HashMap&lt;Animal, String&gt;(); for (Animal animal : list) { names.put(animal, animal.getName()); // i assume there is a getName method } return names; } </code></pre> <p>With the first method if you pass in an List of Cats you get a Map with Cat as key. The second method would always return a Map with general Animal key.</p> <p>By the way this is not valid java syntax:</p> <pre><code>public &lt;? extends Animal&gt; void takeThing(ArrayList&lt;?&gt; list) </code></pre> <p>Using this form of generic method declaration you have to use a valid java identifier and not "?".</p> <p>Edit:</p> <p>The form "? extends Type" only applies to variable or parameter type declaration. Within a generic method declration it has to be "Identifier extends Type" as you are able to refer to the "Identifier" from within your method.</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.
 

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