Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Language specification: Why can't I assign list of string to list  of object?

Thread view: 
Claudio Nieder - 14 Apr 2007 17:42 GMT
Hi,

I'm interested in any reference to discussions, why the
language specification was made so that

"Subtyping does not extend through generic types: T <: U does not imply
that C<T> <: C<U>."
(JLS chapter 4.10)

As seen in this example:

$ cat G.java; javac -Xlint G.java
import java.util.List;
class G {
List<String> ls; List<Object> lo; List l; String s;
public void m() { lo.add(s); lo=ls; lo=(List<Object>)ls; l=ls; lo=l; }
}
G.java:4: incompatible types
found   : java.util.List<java.lang.String>
required: java.util.List<java.lang.Object>
public void m() { lo.add(s); lo=ls; lo=(List<Object>)ls; l=ls; lo=l; }
                                ^
G.java:4: inconvertible types
found   : java.util.List<java.lang.String>
required: java.util.List<java.lang.Object>
public void m() { lo.add(s); lo=ls; lo=(List<Object>)ls; l=ls; lo=l; }
                                                     ^
G.java:4: warning: [unchecked] unchecked conversion
found   : java.util.List
required: java.util.List<java.lang.Object>
public void m() { lo.add(s); lo=ls; lo=(List<Object>)ls; l=ls; lo=l; }
                                                                  ^
2 errors
1 warning

it is no possible to assign the list of strings to the list of objects.

I find this behaviour quite annoying - even more so as the Java language
specification allows the workaround via the non-generic list "for the sake
of compatibility with older code" - that I wonder what kind of expected
problems led the designers of genericity in Java to disallow the direct
assignment of e.g. a list of string to a list of object.

If you can point me to any discussions about this on the web I would
appreciate it.

Thank you very much,
claudio
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

Stefan Ram - 14 Apr 2007 17:58 GMT
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.