This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
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

String

 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody guide on Why String class is final?
What effect it would have created if it would have not been final?

Thanks in advance.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only String but all other Wrapper class are final.
It is because:
1)A standard class is created with standard functionalities.
2)Any function which needs to be override a new class will be created keeping the main functionality intact.
3) String class work on fly-design pattern.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read through the API and it tells you that Strings are immutable. If it were possible to extend the String class it might cease to me immutable.
It would then be possible to have objects which return true from obj instanceof String and equals but do not return the same hash code all the time. It would also be possible to have two objects which start off returning true from equals and later on return false (or vice versa).

People (and the JVM) use Strings on the assumption they are immutable, so all their old code would be "broken."
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Kathy's book I fond the explanation sbout the same question:

If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String's value.
You might say, "Well that's all well and good, but what if someone overrides the String class functionality; couldn't that cause problems in the pool?" That's one of the main reasons that the String class is marked final. Nobody can override the behaviors of any of the String methods, so you can rest assured that the String objects you are counting on to be immutable will, in fact, be immutable.


So can anybody please explain on the basis of the above?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sumukh Deshpande:


So can anybody please explain on the basis of the above?


Not sure what you want, here -- you've got a good explanation already. What more do you want to hear?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic