Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Language specification: Why can't I assign list of string to listof object?

Reply
Thread Tools

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

 
 
Claudio Nieder
Guest
Posts: n/a
 
      04-14-2007
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
--
Claudio Nieder, Kanalweg 1, CH-8610 Uster, Tel +41 79 357 6743
yahoo messenger: claudionieder aim: claudionieder icq:42315212
mailto(E-Mail Removed) http://www.claudio.ch
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-14-2007
Claudio Nieder <(E-Mail Removed)> writes:
>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 );

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      04-14-2007
Claudio Nieder <(E-Mail Removed)> 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.)

--
Lew
 
Reply With Quote
 
claudio.nieder@gmail.com
Guest
Posts: n/a
 
      04-16-2007
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

 
Reply With Quote
 
claudio.nieder@gmail.com
Guest
Posts: n/a
 
      04-16-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating an (inefficent) alternating regular expression from a listof options metaperl.com Python 7 09-18-2008 08:20 PM
Advanced XSLT: Creating a two-column HTML table from an unsorted listof XML nodes Options Eric XML 1 02-29-2008 10:19 AM
To give LISTof Wirelss Networks,which Win32 cmd in cmd prompt is u =?Utf-8?B?U2V6aW4gRXJlbiAocy5lcmVuQHRldGFzLmNvbS50cik=?= Wireless Networking 1 07-05-2007 04:42 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments