This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generics Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics Question" Watch "Generics Question" New topic
Author

Generics Question

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
In the method below, do I need to use <String> as it is in the method declaration of the second example of the same method?


Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You don't need to.
You might return a List<?> or a List<? super String>.
There isn't much point to returning a List<? extends String> even though the compiler will permit it. It's all up to the definition of the contract that you are providing to your clients.


Tony Morris
Java Q&A (FAQ, Trivia)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Note that java.lang.String is a final class (you can't subclass it), so List<? extends String> or List<? super String> isn't necessary.

Just use List<String> as in your second version of the method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jesper de Jong:
Note that java.lang.String is a final class (you can't subclass it), so List<? extends String> or List<? super String> isn't necessary.

Just use List<String> as in your second version of the method.


List<? extends String> makes no sense.
List<? super String> may certainly be sensible in some given context.
I said that already.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question
 
Similar Threads
Extracting A Value From Hashmap
Collection question
Filepath Help
setValueAt() and JTable
Initializing Arraylists inside a loop