Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why String class is final  RSS feed

 
bikasit babu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why string class is final?
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because String class is immutable and you are not supposed to modify all it methods.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bikasit babu wrote:why string class is final?

The string class isn't anything at all, because it doesn't exist. It's String.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bikasit babu wrote:why string class is final?


The most basic reason behind String class to be kept final is probably the multithreading issue. By being final, Java maker ensured that, operation involving String class are Thread-Safe..
What I mean is that, no Thread can pass a String object to another Thread, without building it completely.

Another reason for this may be: -
String class contains lots of methods that the Java maker didn't wanted to be modified.
Because some creepy developer might overload the String class, and provide his own implementation of some pre-defined method that can make his life hell..

For Example: -
Methods like contains(), compare(), startWith(), length(), etc,
So, if someone redefines these methodss in his own class subclassing the String class, then he can get some underiable results..

Guillaume Jourdan wrote:
String class is final because, they are immutable..


Well, actually it is other way round.. String objects are immutable because, String class is final.. And that is that Bikasut is asking... Why is that??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Well, actually it is other way round.. String objects are immutable because, String class is final.. And that is that Bikasut is asking... Why is that??

No, it's not the other way around - the String class is designed to be immutable, and to make sure that String objects are always immutable it's necessary to make the class final. Because if it wasn't final, people would be able to extend it and make it mutable again (by adding mutable properties, for example).

Note that making a class 'final' is not sufficient for making it immutable - there are other criteria that have to be met before a class is immutable.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Well, actually it is other way round.. String objects are immutable because, String class is final.. And that is that Bikasut is asking... Why is that??

Hmmm, not sure I agree with that. String isn't immutable because it's final (although it doesn't hurt). There are a few examples of immutable classes that aren't - BigInteger and BigDecimal being just two - and plenty of examples of final classes that are not immutable.

@bikasit - The actual answer to your question is: 'because the developer of the class didn't want you to subclass it'.
Everything else is pure speculation because we don't know what s/he was thinking when they wrote it; but immutability probably has a lot to do with it.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
R. Jain wrote:Well, actually it is other way round.. String objects are immutable because, String class is final.. And that is that Bikasut is asking... Why is that??

No, it's not the other way around - the String class is designed to be immutable, and to make sure that String objects are always immutable it's necessary to make the class final. Because if it wasn't final, people would be able to extend it and make it mutable again (by adding mutable properties, for example).

Note that making a class 'final' is not sufficient for making it immutable - there are other criteria that have to be met before a class is immutable.


Ok.. Quite a valuable information I missed out.. Thanks Jesper
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question "why is class String immutable" has been asked many times - if you do a search, you can find many previous discussions about this.

One of the reasons is that it allows a number of optimizations, such as the String pool mechanism.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!