Here is what I know:
Double
is a subtype of Number
and List<Double>
is not a subtype of List<Number>
.List<Dog>
is not a subtype of List<Animal>
because you can add Cat
to List<Animal>
but you can't do that with List<Dog>
.List<? extends Number>
means this list can store variables of type Number and variables of subtype of Number. List<Double>
means this list can store variables of type Double.Please correct me if anything above is wrong and then Is List<Double>
a subtype of List<? extends Number>
and why?
There are a few points that should also be added
At runtime List<Double>
, List<? extends Number>
, List<?>
and List<Object>
are all identical. The generic parameter is not compiled at all. All the magic with generics is all compile time fun. This also means that if you have a List
that is empty you have no idea what the generic parameter is!
Try not to think of the generic parameter as a "Subtype", the generic parameter really means "the class uses a generic parameter" so in this case "the list uses a Number". A good example of this is the HashMap source, if you have a look into the inner workings of it, it is actually storing an array of Entry
, and the entries all have keys and values stored on them. When you look at more complex uses of generics you occasionally see this sort of use.
In the situation of a List
the generic parameter means that the list stores that type of object, it could be that the object never stores an object of the type of the generic parameter at all! Like this:
public class DummyIterator<O> implements Iterator<O>{
public boolean hasNext() {
return false;
}
public O next() {
return null;
}
}
What does List<? extends Number>
actually mean? Well it is pretty much the same as List<Number>
for most uses. Keep in mind though that by saying ?
you are pretty much saying 'I don't care about the type' which shows itself in this situation:
List<Double> doubles = new ArrayList<Double>();
List<? extends Number> numbers = doubles;
numbers.add(new Double(1)); //COMPILE ERROR
Number num = numbers.get(0);
So we can't add a Double
to a <? extends Number>
. But for this example:
List<Double> doubles = new ArrayList<Double>();
List<Number> numbers = doubles; //COMPILE ERROR
numbers.add(new Integer(1));
Number num = numbers.get(0);
You can't assign the List<Double>
to a List<Number>
which makes sense as you are specifically telling it, that lists use only Number types
So where should you use a ?
? well really anywhere you could say "I don't care about the generic parameter" so for instance:
boolean equalListSizes(List<?> list1, List<?> list2) {
return list1.size() == list2.size();
}
You would use the ? extends Number
type of format only where you are not modifying the object using the generic parameter. so for instance:
Number firstItem(List<? extends Number> list1) {
return list1.get(0);
}
Instead of using the ?
and ? extends Number
formats try using generics on the class / method instead, in most cases it makes your code more readable as well!:
<T extends Number> T firstItem(List<T> list1) {
return list1.get(0);
}
Class:
class Animal{}
class Dog extends Animal{}
class AnimalHouse<A extends Animal> {
List<A> animalsInside = new ArrayList<A>();
void enterHouse(A animal){
animalsInside.add(A);
}
A leaveHouse() {
return animalsInside.remove(0);
}
}
AnimalHouse<Dog> ah = new AnimalHouse<Dog>();
ah.enterHouse(new Dog());
Dog rufus = ah.leaveHouse();
As a bonus thought around generics you can also parameterise methods to return a particular class. A good example of this is the any() method in junit and the empty list collection:
Dog rufus = Matchers.<Dog>any();
List<Dog> dogs = Collections.<Dog>emptyList();
This syntax allows you to specify the return type of an object. Sometimes quite useful to know (makes some casting redundant)!