String & StringBuffer

Hi,Why String objects are Immutable?and StringBuffer objects are mutable.what is the logic behind this design.thanks in advance.
[163 byte] By [N.R.Raoa] at [2007-10-3 10:23:42]
# 1

Strings are immutable because it makes no sense to make them mutable. strings in real life are immutable:

is the word "mutable" the same as the word "camera", only with the letters changed, or is it a new, different word?

StringBuffer is mutable because otherwise it wouldn't work. you have to be able to add to it, and change it, by definition. that's what it's for

georgemca at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...
# 2
why not?=)
Redxxiva at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...
# 3
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.htmlread this
G_Abubakra at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...
# 4

My theory on this is that Strings occupy fixed contiguous areas of memory. To alter them may corrupt the area of memory that comes immeadiately after a particular String. Therefore it is safe to not let programmers to alter them. A StringBuffer on the other hand overcomes this restriction and will allow expansion of itself by the programmer to other areas of memory, under control of the compiler, when required.

ChrisLesliea at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...
# 5

I wish people would stop thinking of StringBuffer and StringBuilder as special kinds of Strings

the answer is that all raw data is inherently immutable. you can't change the value of 2 to be 1, you can only use 1 instead of 2.

of course, there are ways to mutate a string instance.....

georgemca at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...
# 6
Hope this URl will help you. http://www.acooke.org/andrew/immutable.html
Karthicka at 2007-7-15 5:45:29 > top of Java-index,Java Essentials,Java Programming...