• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why String class is final

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why string class is final?
 
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because String class is immutable and you are not supposed to modify all it methods.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic