• 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

why String class is immutable?

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi String class has got final class and private variables so it's state is not changed
In the sam way Stringbuffer has also got finla class and private variables but it is not called as immutable class ,why?

:roll:

can any one please justiy
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
short form: read the API documentation

long form: String is immutable for the following reasons:

1) Strings are commonly used as keys within hashes
2) Strings are given special treatment by the compiler; for example string literals are cached and reused.

StringBuffer is more a container class for Strings than a stand-in for String.

Integer, Float, Character etcetera are also immutable.

Jeremy
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you're asking why String was made immutable and StringBuffer was not, or why String is said to be immutable, while StringBuffer is not. If it's the first, then you don't know what StringBuffer is for; in the latter case, it's that you don't know what "immutable" means. Based on history, I'm going to go with the second alternative.

"Immutable" just means "not changeable". Go look at every single method of String, and you won't find a single one that observably changes the state of the object it's called on. The "value" of a String never changes once a String object is created. As Jeremy says, there are lots of reasons why Strings guaranteed to never change are useful.

On the other hand, there are many methods that change the state of a StringBuffer: all the append() methods, setLength(), delete(), the insertXXX() methods, etc, etc. Changing the contents is the whole point of StringBuffer -- an immutable StringBuffer class would be useless!
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think of the immutability of Strings with regards to how String data is stored in memory.

String s1 = new String("ExamScam");

s1 points to a memory location containing the string ExamScam. If someone changes the string:

s1 = examscam.com;

Then a whole new memory location is used, and s1 is pointing at a completely new memory location. You can't adjust or fiddle with the memory location where a String stores its character data. You can with just about every other object class in Java.

That's sorta how I think about the immutability of the class. Certainly the term 'immutable' with regards to Strings goes beyond the simple fact that they can't be extended due to the use of the keyword final.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saikrishna cinux:
hi String class has got final class and private variables so it's state is not changed
In the sam way Stringbuffer has also got finla class and private variables but it is not called as immutable class ,why?

:roll:

can any one please justiy



Hai,

Actaully String class is using the FLYWEIGHT pattern inside. With the usage of the pattern one can accomadate more objects with same data. Let me put in this way, for example assume 100 people are accessing a application, in that first person created a string hello, and if other 99 people are also trying to create the work hello, there will be only one word hello in the pool and all the references will be pointed to it.

So it is not good to change this as every one is pointing to this one. Thats why strings are made immutable.

Hope I have given clear explanation.



reply
    Bookmark Topic Watch Topic
  • New Topic