Originally posted by Manfred Leonhardt:
Immutable means that it can't be changed once created. That means that if we have the following:
String s1 = new String( "First" );
The string object "First" can never be changed. I can however change the reference object s1 to point to another string.
s1 += " One";
The object s1 will now point to a string: "First One".
The string "First" will probably be garbage collected by JVM because no references to it exist!
On the other hand, using StringBuffer we can actually change the object contents that the stringbuffer references.
StringBuffer sb = new StringBuffer("First");
Now we no longer have a stringbuffer containing "First" we only have a stringbuffer "First One". In other words, we have changed the string buffer contents.
Immutable --> contents can not be changed
Mutable --> contents can be changed