this is from HeadFirst Java: ( page 575 )
This:
public void takeThing(ArrayList list)
Does the same thing as this:
public void takeThing(ArrayList extends Animal> list)
So here is my question: if they are exactly same, why don't we write
public extends Animal> void takeThing(ArrayList> list)
or
public void takeThing(ArrayList list)
Also, when would it be useful to use a ? instead of a T in a method declaration ( as above ) with Generics, or for a Class declaration? What are the benefits?
解决方案
The big difference between
public void takeThing(ArrayList list)
and
public void takeThing(ArrayList extends Animal> list)
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.
Here a more complex example to illustrate this:
// here i can return the concrete type that was passed in
public Map getNamesMap(ArrayList list) {
Map names = new HashMap();
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 getNamesMap(ArrayList extends Animal> list) {
Map names = new HashMap();
for (Animal animal : list) {
names.put(animal, animal.getName()); // i assume there is a getName method
}
return names;
}
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.
By the way this is not valid java syntax:
public extends Animal> void takeThing(ArrayList> list)
Using this form of generic method declaration you have to use a valid java identifier and not "?".
Edit:
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.