This discussion is archived
12 Replies Latest reply: Mar 26, 2010 3:33 AM by 843793 RSS

? extends String

843793 Newbie
Currently Being Moderated
public void f(List<? extends String> list) 
{
     list.add("s");    //1
}
why there is error at line 1?
what kind of object this list can take?
  • 1. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    [Obligatory reading|http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html]
  • 2. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    elOpalo wrote:
    [Obligatory reading|http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html]
    then what kind of object can it add
    or it can add anything or not
  • 3. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    Have You read the link I provided?

    There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:
        
    public void addRectangle(List<? extends Shape> shapes) {
            shapes.add(0, new Rectangle()); // Compile-time error!
    }
    You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
  • 4. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    Also: "? extends String" is useless, since String if final and nothing except String could possibly match that wildcard.
  • 5. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    ? extends Object
    also deos not work
  • 6. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    ? extends Object
    also deos not work
  • 7. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    quenchjava wrote:
    ? extends Object
    also deos not work
    Which is explained in the link you've been given. Read it! Read it again!

    One effect of wildcards is that you can't anything to a Collection that has a wildcard type.
  • 8. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    JoachimSauer wrote: Read it! Read it again!
    From the beginning till the end with understanding.
  • 9. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    JoachimSauer wrote:
    Which is explained in the link you've been given. Read it! Read it again!

    One effect of wildcards is that you can't anything to a Collection that has a wildcard type.
    If that was supposed to read "[o]ne effect of wildcards is that you can't [add] anything to a Collection that has a wildcard type" you should reread the tutorial too.. [;)|http://forums.sun.com/thread.jspa?messageID=10896792#10896792]

    With kind regards
    Ben
  • 10. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    BenSchulz wrote:
    If that was supposed to read "[o]ne effect of wildcards is that you can't [add] anything to a Collection that has a wildcard type" you should reread the tutorial too.. [;)|http://forums.sun.com/thread.jspa?messageID=10896792#10896792]
    Meh ... "can't add anything **interesting** to a Collection" ;-)

    Yeah, but you're right of course.

    Edit: I always ignore/forget this one, because I see no good reason ever to add null to a Collection, especially not if you explicitly use the null literal.
  • 11. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    JoachimSauer wrote:
    Meh ... "can't add anything **interesting** to a Collection" ;-)

    Yeah, but you're right of course.

    Edit: I always ignore/forget this one, because I see no good reason ever to add null to a Collection, especially not if you explicitly use the null literal.
    I agree, I just like to remind people. Too frequently the words "read-only" and "wildcard" appear in the same sentence. The obvious exceptions to that rule are clear()-methods, the oft-forgotten add(null) is less obvious and then there's wildcard capture which people are so aware of it might as well not exist.

    The reminder was not so much for you as for the OP and anyone who might stop by in future. The other part was just to tease, that's always fun. ;)

    With kind regards
    Ben
  • 12. Re: ? extends String
    843793 Newbie
    Currently Being Moderated
    BenSchulz wrote:
    The other part was just to tease, that's always fun. ;)
    I know! And sometimes I'm easily teasable.