
Signature
Claudio Nieder, Kanalweg 1, CH-8610 Uster, Tel +41 79 357 6743
yahoo messenger: claudionieder aim: claudionieder icq:42315212
mailto:private@claudio.ch http://www.claudio.ch
>it is no possible to assign the list of strings to the list of objects.
Assume,
java.util.List<java.lang.String> stringList =
new java.util.ArrayList<java.lang.String>();
Now assume, the following assignement would be allowed:
java.util.List<java.lang.Object> objectList = stringList;
Then one could add an object to this »objectList«:
objectList.add( new java.lang.Object(){} );
However, this would break the type of »stringList«, because
the following call to the get-Operation of the stringList now
will not return a string, because we have been allowed to add
an object above:
java.lang.String string = stringList.get( 0 );
Lew - 14 Apr 2007 20:11 GMT
Claudio Nieder <private@claudio.ch> writes:
>> it is no possible to assign the list of strings to the list of objects.
There is a fair treatment of this matter in
<http://java.sun.com/docs/books/tutorial/extra/generics/subtype.html>
and the following chapter about wildcards.
The Sun Tutorial is your friend. (STIYF.)

Signature
Lew
claudio.nieder@gmail.com - 16 Apr 2007 22:30 GMT
Hi,
> There is a fair treatment of this matter in
> <http://java.sun.com/docs/books/tutorial/extra/generics/subtype.html>
>
> and the following chapter about wildcards.
>From which I cite:
"And here is a naive attempt at writing it using generics ...
List<Object> ..."
Exactly what I was doing wrong.
Thank you for you reference. It seems to be time for me to read
through some tutorials again.
claudio
claudio.nieder@gmail.com - 16 Apr 2007 22:30 GMT
Hi,
> objectList.add( newjava.lang.Object(){} );
ARGH... now I feel very dumb indeed, for not spotting this. Doubly
dumb even.
First, because in my application I was so focused on how can I make a
method which
could handle all kind of lists, i.e. read from the list, that I did
not notice how
much harm and damage can be done by writing wrong stuff to it.
Secondly because even though I read all the relevant chapters in the
JLS it never
occurred to me, that I was wrongliy using List<Object> as method
parameter where
I should have used List<?>.
It made click only after I read your answer for which I'm thankful!
claudio