As per my knowledge,
a mutable string can be changed, and
an immutable string cannot be changed.
Here I want to change the value of String like this,
String str = "Good"; str = str + " Morning";
In the above code you create 3
Note: Strings are always immutable. There is no, such thing as a mutable String.
str is just a reference which eventually points to "Good Morning". You are actually, not working on
1 object. you have
StringBuffer str = new StringBuffer("Good"); str.append(" Morning");
StringBuffer contains an array of characters. It is not same as a
The above code adds characters to the existing array. Effectively,
StringBuffer is mutable, its
String representation isn't.