Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String and Generics

 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
given that String is final are these two things the same

List<String>
List<? extends String>

if possible you can explain that with a piece of code
 
siv sanny
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<? extends String>: something extents String. but there can never be a something that can extend String(final class) except String itself. so it would be effectively equal to List<String>.



-sankar
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<? extends String> is worse than List<String>.

Because technically the compiler doesn't know what exact subclass of String is used, you cannot add anything to this List, whereas you can with List<String>.

Overall:
List<? extends String>: adding anything is not possible, any return value is String.
List<? super String>: adding Strings is not a problem, but the return value can only be assigned to an Object reference without casting.
List<String>: adding Strings is not a problem, any return value is String.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Rob Prime]:

List<? extends String> is worse than List<String>.

Because technically the compiler doesn't know what exact subclass of String is used, you cannot add anything to this List, whereas you can with List<String>.


Some people use constructs like List<? extends String> intentionally, to create a read-only List which is enforced at compile time, rather than throwing an UnsupportedOperationException at run time. Anyone trying to add or set an element in the list will get a compiler error. Unfortunately the error is something like this:

which is less clear than it might be. And the original "List<? extends String>" is not a very clear way to communicate the concept of an unmodifiable list, either. So I hope this practice doesn't catch on widely. It does exist, however.
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
A question based on your previous reply.
For,

we cannot do a sr.add(), and you said it is done intentionally to make the list read only.
but this is not enforced, right ?
because you can do a

One way Im aware of to make unmodifiable collection is to use wrappers.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[praveen]: we cannot do a sr.add(), and you said it is done intentionally to make the list read only.

It could be intentional, yes.

[praveen]: but this is not enforced, right ?

It's partly enforced, but there are ways around it, as you show.

[praveen]: One way Im aware of to make unmodifiable collection is to use wrappers.

Yes, that gives more secure enforcement of unmodifiability, at run time. You could combine the two techniques, and get an unmodifiable collection that is always unmodifiable at run time, and usually modifiable at compile time:

That provides more compile-time enforcement than a plain unmodifiableList() does, at least. But it's still not very readable. I doubt I'd ever use it.
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt if I'd ever use that either.
thanks Jim.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic