aspose file tools*
The moose likes Java in General and the fly likes String is immutable:value cannot be changed.but Why has it been made so? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String is immutable:value cannot be changed.but Why has it been made so?" Watch "String is immutable:value cannot be changed.but Why has it been made so?" New topic
Author

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

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 663
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

Search these forums. This question has been asked many times.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 663
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: May 22, 2012
Posts: 877
    
    5
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 663
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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Ivan Jozsef Balazs
    Rancher

    Joined: May 22, 2012
    Posts: 877
        
        5
    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

    Joined: Mar 22, 2000
    Posts: 12825
        
        5
    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

    Joined: May 22, 2012
    Posts: 877
        
        5
    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: String is immutable:value cannot be changed.but Why has it been made so?