• 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

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search these forums. This question has been asked many times.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"...
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1044
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Author and all-around good cowpoke
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 1044
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic