• 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
Sheriff
Posts: 15758
74
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!