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

C

Claudio Nieder

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
 
S

Stefan Ram

Claudio Nieder said:
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 );
 
C

claudio.nieder

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top