File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Just came across a statement public void takeList(List<?  extends String) list {} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just came across a statement public void takeList(List<?  extends String) list {}" Watch "Just came across a statement public void takeList(List<?  extends String) list {}" New topic
Author

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

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Just came across a statement public void takeList(List<? extends String) list {} what does the ? mean in here


http://www.lifesbizzare.blogspot.com || OCJP:81%
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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

Joined: Oct 13, 2005
Posts: 40029
    
  28
I don't think you can add to anything taking <?....> as an actual type parameter.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Thank you for sorting that out Rob. You can only add null to a Collection<? extends Foo>.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just came across a statement public void takeList(List<? extends String) list {}