If the <code>trim()</code> method has no effect on the string it is operating on,
a new string will not be created, instead it returns the reference to the same string. Hence both == and <code>.equals()</code> returns true.
Consider for example the string "Hello World". Since this string has no leading or trailing spaces, calling the <code>.trim()</code> method on this string object has no effect on it because there is nothing to trim!!. In such a case, the <code>trim()</code> method will give you back the reference to the same object. Hence
- all the lines above will print true.
Note that it really doesnot matter if the string is created using the new() object or using the = operator. ie., whether the string is in the string pool OR whether it is on the heap. Since we are only talking about references here, it is sufficient to say the two references are equal.
On the other hand, if the string
does have leading and/or trailing spaces, then the <code>trim()</code> method returns you a reference to a different object that has spaces trimmed off. Ofcourse in that case both reference comparison == and content comparison <code>.equals</code> returns false because we are comapring two different objects.
What's more..you can observe the same behaviour with the other 'pseudo-mutator' methods in the String class - <code>toLowerCase(), toUpperCase() , trim() </code>. All of them returns the reference to the string object on which the method is being invoked
if the method has no effect on it. For example, calling <code>("HELLO WORLD").toUpper()</code> would return the reference to the same string because all the characters in the string in question are already in upper case!!
I will leave the rest to your imagination......

I would like to emphasize this is a
really important concept for the sake of
SCJP exam. Write a LOT of code to help you grasp the concept.
Hope that helps,
Ajith