Possible Duplicate:
What's the advantage of a String be Immutable?
Strings are Immutable?
String s = "PROJECT";
Here s cannot be modified. But String s2=s.toLowerCase() will return me a new string. Here still I can get a new copy of the original string! But if Java still wants immutability, then why not restrict the user while modifying the string (throw an exception or something). Then why immutability? Can any one explain why?
Strings are immutable, meaning that this is always true:
In comparison, consider what would happen if Strings were mutable.
This example may seem trivial, but it means that unless you actually re-assign the reference
sthen you can guarantee thatswill not be changed by any piece of code.