• 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 is final ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
friends,
What's the reason behind making String immutable.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Performance. Immutable strings can be placed in a string pool. Final classes and methods don't participate in polymorphism and can be inlined at compile time.
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere that it also has to do something with security. So, someone cant write a String subclass and get at some secret information stored in Strings, something like that...
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider

It would be good if nobody could make such a SpyString and pass it into code that might be expected to compare the it to some confidential information, like a password or PIN.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class being final is different then the fact that strings are immutable.
Being final means that the String class cannot be subclassed. And the fact the strings are immutable means that once a String has been created it can't be changed. You can only what the reference points to.
so
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. I was trying to speak to the earlier point of subclassing. Since I don't think the question has been answered yet...
One reason for making string immutable:
Suppose a string is passed to an object which needs to save the the information in that string for future use. If the string were not immutable, it would need to make a copy of the information in string, at extra cost in memory and time, or save a reference to the string and run the risk that somebody else will change the value of the string in the future, which is generally unacceptable. Thus we need an immutable String class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic