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

Discussion in 'Java' started by Claudio Nieder, Apr 14, 2007.

  1. 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:p http://www.claudio.ch
    Claudio Nieder, Apr 14, 2007
    #1
    1. Advertising

  2. Claudio Nieder

    Stefan Ram Guest

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

    Claudio Nieder <> 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 );
    Stefan Ram, Apr 14, 2007
    #2
    1. Advertising

  3. Claudio Nieder

    Lew Guest

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

    Claudio Nieder <> 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
    Lew, Apr 14, 2007
    #3
  4. Claudio Nieder

    Guest

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

    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
    , Apr 16, 2007
    #4
  5. Claudio Nieder

    Guest

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

    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
    , Apr 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    6
    Views:
    436
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  3. Eric
    Replies:
    1
    Views:
    2,035
    Pavel Lepin
    Feb 29, 2008
  4. metaperl.com
    Replies:
    7
    Views:
    256
    metaperl.com
    Sep 18, 2008
  5. Tobia Conforto
    Replies:
    5
    Views:
    221
    Tobia Conforto
    Nov 10, 2012
Loading...

Share This Page