One basic thing to know is that String instances are immutable, i.e. once created there is no way to change the value it contains. Every instance method called onto a String object will return ANOTHER String reference if the content of the object has to be changed. For instance,
String t = "String1";
String s = t.trim();
In this case, t and s refer TO THE SAME STRING object in memory since nothing had to be "changed" in the String "String1". When I say "changed", I do NOT mean that the String itself would have been changed if necessary but merely that if some changes were required then a new String would have been created and assigned to s.
In this example two distinct instances of String exist, because one space has to be removed from t and thus a new String had to be created, so both references refer to distinct memory area.
String t = "String1 ";
String s = t.trim();
Now the second basic thing to know is that a StringBuffer implements a mutable version of the String class, i.e. a StringBuffer, once created, may be changed by mutator methods, and if the method returns a StringBuffer object then it is the same StringBuffer object than the one on which the method has been invoked (in the case of an instance method), i.e this StringBuffer.
I really hope it helps because these concepts are very central to
Java and anyone has to master them...
Let us know if you need more help
Val