what is mutable and immutable in strings

Hi,Everybodyi am not getting what exactly mean by mutable and immutable in stringsString is immutable and StringBuffer is a mutable.Can any one reply to this question.
[195 byte] By [Thomosa] at [2007-11-26 19:16:13]
# 1
Yes. And StringBuffers aren't Strings.One is immutable and the other isn't. No conflict.
paulcwa at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 2
> String is immutable and StringBuffer is a mutable.Do you have lots of questions like this? You should in google in that case. It's very easy to find answers to these easy common questions.The contents of a string can't be changed. Kaj
kajbja at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 3
Immutability: an instance's observable state cannot be modified.
CeciNEstPasUnProgrammeura at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 4
i know the answer but i want to know clear picture on this.How the contents can be altered..?string s="test";s="test"+"how";Sop(s);it prints testhowi need the cleat picture on this.
Thomosa at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 5

> it prints testhow

[ _ ] You are aware that = is the assignment operator.

[ _ ] You know that "String1 + String2" compiles to "new StringBuffer(String1).append(String2).toString())"

The point of immutability is that you can not alter contents. Strings are immutable. Nothing you can do short of reflection changes a string's value.

CeciNEstPasUnProgrammeura at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 6
> s="test"+"how";That creates a new string and s will reference that new string.Kaj
kajbja at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 7

consider string s

when u say "string s;".......that becomes a reference...

now wen u say s="test"....it creates an object(u need not say new String for this class)

now if u say .....

s="test22"

the reference of s now points to test22 and the earlier refernce of test is soon garbage collected...

StringBuffer is mutable ....coz u can append to it or update...or watever the methods allow u to do..

so...

StringBuffer s=new StringBuffer("pass some string");

s.append("hello");

so this now gives s as "pass some stringhello";

hope u get that :)

sweta_da at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 8

> now wen u say s="test"....it creates an object

No, it doesn't. Since "test" is a string literal, the String object was created when the class was loaded. The above statement simply assigns a reference to the already existing String to the variable s.

> now if u say .....

> s="test22"

> the reference of s now points to test22 and the

> earlier refernce of test is soon garbage

> collected...

No. References are not GCed. Only objects are GCed. String objects in the constant pool ("test", above) are not GCed.

> StringBuffer is mutable ....coz u can append to it or

> update...or watever the methods allow u to do..

> so...

Mutable means you can change the state. You can change the state of StringBuffer, but not of String.

jverda at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 9
thanxx......:)for the info
sweta_da at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 10
but then how wat happens to the earlier value in the string...hhere "test"?
sweta_da at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 11

Theres a thing in java called the literal pool where strings are stored, each time you create a string that is unique a new object is created.

So if you have a string that says test and new object with the content test is put in the literal pool, say you add the word new to it, then a new object in the literal pool will be created that says newpool and pointer will refer to that. The funny thing about the literal pool is, with int if you say int a = 1 and int b = 1 and you do the argument if int a = int b the answer will be false and they are two different variables, they are .equals though, but since strings are stored in the literal pool, if you say string a = pete and string b = pete and you says if string a = string b the answer will be true as they are both pointing to the same object in the literal pool. Bit confusing i know, but thats how it works.

cabbagesa at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...
# 12

> The funny thing about the

> literal pool is, with int if you say int a = 1 and

> int b = 1 and you do the argument if int a = int b

> the answer will be false and they are two different

> variables, t

The funny thing is that this is utterly wrong. ints are primitives, and thus == tests for equality and not for referential identity.

int a = 1;

int b = 1;

System.out.println(a == b); // will print true

> if you say string a = pete and string b = pete and you says if string a = > string b the answer will be true

String a = "pete";

String b = new String("pete");

System.out.println(a == b); // false..

Not to contradict, but to show the difference.

CeciNEstPasUnProgrammeura at 2007-7-9 21:29:04 > top of Java-index,Java Essentials,Java Programming...