posted 18 years ago
Immutable means " Once you have assigned a String a value, that value can never change "— it's immutable,
frozen solid.while the String object is immutable, its reference variable is not means it can refer to another String object if it's diected to refer another String object leaving previous String object unchanged.
String s = "abcdef"; // create a new String object, with
// value "abcdef", refer s to it
String s2 = s; // create a 2nd reference variable
// referring to the same String
// create a new String object, with value "abcdef more stuff",
// refer s to it. (Change s's reference from the old String
// to the new String.) ( Remember s2 is still referring to
// the original "abcdef" String.)
s = s.concat(" more stuff");
The main advantage of String object's immutability is that there may be many references to single String object
and if any reference changes the String object it'll reflect to all other references,which is not a good idea so in case any referense changes contents of String object a new String object is created and reference is directed to newly created String.
i hope this will help you..
Regards