Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic