• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Mutability

 
Pankaj Shet
Ranch Hand
Posts: 297
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I want to know why are Strings Immutable?
I know that state of String object cannot be changed.
But this is the meaning of immutability. I want to know why cant the state of String objects be changed? what is the purpose of String immutabilty?
Help is always appreciated.
Thanks and Regards,
-Pankaj.
 
Stephan van Hulst
Bartender
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly for ease of use and security, I figure.

Immutable types are very simple, and make it much easier for programmers to reason about the correctness of a program. You can easily pass Strings in and out of methods without worrying that internal state of an object is compromised, or that the String changes halfway through the execution of a method operating on it. Immutable types make it harder for a program to break, or for a malicious person to break it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic