posted 16 years ago
Sorry, cowboys,
this answer is not quite correct.
In a
List <? super Integer> al = new ArrayList<Number>();
you can not add an Object. Would be a compiler error.
You can't even add a Number. Only Integers.
You may assign al to an ArrayList<Integer>, <Number> or <Object>, but in all these lists you can add only Integers.
Because al's type is unknown. Known is only that the smallest it can store is Integer. Therefore the highest you can put in must also be Integer, otherwise you may get class cast exceptions. E.g. in the case when you assign it to an ArrayList<Integer>, which is perfectly legal, you also may only store Integers in it.
Therefore lines 2 and 3 are legal. Integers can be stored properly.
Things change when you look on the types, that you can get out of it:
They're of type Object, because the list is guaranteed to contain any type from Integer upward.
That could be Integer, Number or Object.
To be type safe, the collection must return type Object as well. Integer or Number are too small, because there could be Objects in it.
OK, not in this one, as variable al refers to an object of ArrayList<Number>, but al could easily be re-referenced to another object with type ArrayList<Object>.
Therefore the "running variable" in the for-each loop "no" must also be of type Object. Every other type - like Number in this example - would cause the compiler complaining about the programmer.
Therefore answer d.
And: yes, it is complicated.
Yours,
Bu.
all events occur in real time