Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String is immutable:value cannot be changed.but Why has it been made so?

 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is immutable.Everywhere we read about how spring is immutable.But what I want to know is Why is string immutable. Why have to made string immutable.Had problem would have they faced had string not been immutable.

Thanks.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search these forums. This question has been asked many times.
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I had already searched for this. Everyone has written how string is immutable. Everyone knowing some core java know the string is immutable and everywhere on google they tell how it is immutable but now one talks on why it has been made so.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a quick search on this forum and found dozens of topics for 'Why is String immutable".

The short answer is that nobody here knows the exact reasons why the designers of the language made the choices they did. We can make some guesses, but in the end, that's the best anyone here can do. So, search here, read what folks say, and I'm sure you'll have a better understanding.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need mutable an immutable strings. For some reason they are called StringBuffer/Stringbuilder and String. They could be called say String and ImmutableString .

The next best things after "const"...
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i imagine that when memory space is reserved for the String it is best if that size doesn't change
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

i imagine that when memory space is reserved for the String it is best if that size doesn't change


Why so?

 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:Why so?

Have you had a look at any of the posts that Fred suggested?

However, just a few advantages off the top of my head:
  • 1. It makes Strings Thread-safe.
  • 2. It means that they are safe for use as keys in Maps.
  • 3. It means that their hashcode can be cached as and when needed.
  • 4. It means that their internals can be shared between instances. substring(), for example, does NOT copy the internal character array, it simply creates a new String with a different start offset and length.

  • Also, there is already a mutable "String" class (in fact, two of them) - StringBuilder. So why would you not want String to be immutable?

    My advice: If you're writing a class of your own, make it immutable unless you have a very good reason not to. It just makes life easier.

    Winston
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 972
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston's points shed light on the reasons for the immutability but also for String's being final. Otherwise you could derive from String and undermine it.
     
    William Brogden
    Author and all-around good cowpoke
    Rancher
    Posts: 13056
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Note that the Java designers were very very aware of deficiencies in C which led to many bugs and exploits. Buffer over-runs etc. Immutable Strings prevent certain bugs / exploits that were common in C programs.

    Bill
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 972
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But it is not String's being immutable which prevents buffer overrun and dangling pointers etc. errors in Java.
    You can not make a (n unchecked) buffer overrun error in Java with an array or with a StringBuilder or with a StringBuffer either.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic