Originally posted by Rama Krishna Pathangi:
How can you say that strings are immutable
when you can do an operation like
String motto="Program Once"
motto=motto+"execute everywhere"
motto=motto.concat("dont bet oin it!");
(Plz refer Mughal and rasmussen page 311)
Because none of these operations modify a String object. Rather, each and every one of these creates a
brand new String object. Take this example:
This code snippet generates the following output:
If the original String object had actually been modified, both variables would have referenced the same String. Rather, when the concat method was invoked, a new String was created and returned from that method.
The String object is, indeed, immutable.
Corey