• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just came across a statement public void takeList(List<? extends String) list {}

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just came across a statement public void takeList(List<? extends String) list {} what does the ? mean in here
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means the List will accept any element which subclasses String.
More on generics wildcards here http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html
But remember, can one subclass String?
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:It means the List will accept any element which subclasses String.
More on generics wildcards here http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html
But remember, can one subclass String?


Thankyou with respect to this question i want to know why


list.add("Foo") when added within the method wont compile
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can add to anything taking <?....> as an actual type parameter.
 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:It means the List will accept any element which subclasses String.
More on generics wildcards here http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html
But remember, can one subclass String?


Just trying to be precise, so feel free to ignore. It is the method will take any List that accepts a particular type that is string or subclasses string. It is not the list that accepts any type, the list accepts a specific type.

Now, of course, this example is somewhat silly. Since the String class is final, there are no classes that subclasses string; And the compiler is not smart enough to flag this.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I don't think you can add to anything taking <?....> as an actual type parameter.

With <? super X> you can add anything that IS-A X. With <? extends X> you can only add null.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for sorting that out Rob. You can only add null to a Collection<? extends Foo>.
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Now, of course, this example is somewhat silly.

Somewhat. I think they may be intentionally using the <? extends X> idiom to signal that the list is not intended to be modified, only read. This discourages (though it does not necessarily prevent) people from modifying the List. Not very elegant, but some people do use it this way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic