File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ? extends String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "? extends String" Watch "? extends String" New topic
Author

? extends String

Rodolfo Mendoza
Greenhorn

Joined: Feb 27, 2008
Posts: 14
Hi guys,
Anyone knows why the following code don't work :


The following work fine:


Thanks,
Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49


You cannot add anything to a List that uses <? extends> or <?>
You can add only when the method signature is <? super Dog?

In short

<? extends> - no adding.
<?> - you can pass anytype into the method but still no adding.
<? super> - adding allowed.

Hope that helps. Please correct me if I am wrong.

Uttara Rishi.
Rodolfo Mendoza
Greenhorn

Joined: Feb 27, 2008
Posts: 14
Uttara thanks
You are right...
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
You cannot add anything to a List that uses <? extends> or <?>

Thats correct but with little exception that you can add 'null'.
Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49
Thanks Ahmed.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 664

Uttara Rishi wrote:

<? super> - adding allowed.
.



? super -adding allowed?? i didnt get it are you reffering to super class in this case


http://www.lifesbizzare.blogspot.com || OCJP:81%
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 2466

When you have a List<? extends String> you can read Strings from it, but you can't add anything to it.
When you have a List<? super String> you can add Strings to it, but you can only read Objects from it.
Shahrukh Naqui
Greenhorn

Joined: May 01, 2010
Posts: 14

Hi,

Also remember, for <? super SomeClass> means you can add only an object of SomeClass, or sub type of SomeClass.

This message was edited 1 time. Last update was at by Shahrukh Naqui

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 664

What does ? actually means is it relating to some interface or class

When i tried compiling this code with actual '?' it actually compiled... Is ? some sort of keyword in this case?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 2466

? is the generic wildcard. When you have a List<? extends String>, you could interpret it as "A list of whatever, so long as it extends String".

A Set<?> would be "A set of whatever, I don't care what it is".

http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html
 
 
subject: ? extends String
 
MyEclipse, The Clear Choice