• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Why String are immutable?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why String objects were kept immutable?
Please help.
Thanks & Regards,
Vidyakar Sharma
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String class is not a common class. It has a specific requirements and it is handled as special by both JVM Specification and Java Language Specification. Many language features depend on ability of String to represent constant text. The "side effect" of the immutability of Strings is that it makes the Strings cacheable using hash value. That improve the performance a great deal.

Also, the memory management of Strings is somewhat different than normal objects.
 
Saloon Keeper
Posts: 14010
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most important reason is security.

A lot of security risks would appear if a malicious thread could gain a reference to a mutable String, which is about to be passed into a method that has to validate the String before it performs an important operation. It would be possible for the thread to change the string after it was validated, and then the operation would be carried out using a dangerous String.
 
Ranch Hand
Posts: 32
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another reason of Why String is immutable in Java is to allow String to cache its hashcode.
 
Ranch Hand
Posts: 79
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned above - the most important reason - security & thread safety.

Consider a scenario, in a banking application for money transfer - the beneficiary account number is defined in a string as "0789567345".
If by mistake/intentionally this acc. number is changed, money will go to a wrong account.

Another scenario - if someone change the class name anywhere between processing as ..

getClass().getName().subString(0, 5);

The Class loader will simply say 'Class Not Found'
 
Marshal
Posts: 75873
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Security and thread safety isn't a reason; it's two reasons. If you search the Ranch, you will find how to cheat and change the internal representation of a String object. So security can easily be breached like that. You would use a SecurityManager object to prevent such security breaches. So I don't think security is the principal reason for immutability.
You will find this sort of question is asked frequently; I have just done a search and found this, where you can see several old threads about "why are strings immutable?" It is worth looking at those again.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tekchand Prasad wrote: Another reason of Why String is immutable in Java is to allow String to cache its hashcode.


I can well imagine a mutable class that would cache the hashcode and just mark it on every modification of its contents to be recalculated on next request. (By the way, if the hashcode of a String happens to be 0, which it does even for some ordinary strings, it is in effect not cached and is recalculated on every call.)

I'll throw in another consideration: using mutable Strings as keys in a map would be very error prone. Though maps could be modified to support mutable keys in general (mutable and cloneable objects actually, it gets more muddy every time I think about it), it would incur serious memory and performance penalty.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another important reason, that applies to various classes used as "value objects", is that when using Strings for member variables that are exposed via getter methods, if they were mutable you'd have to keep making defensive copies to avoid breaking encapsulation.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic