Originally posted by kesava narayana:
Yes, every method called on String object creates a new String object and is returned. But this happens only when the returned string is not exactly same as the one on which it is called.
Do note that this bolded statement is technically no longer guaranteed as of
Java 5. This was true for older versions of the Java API, but it's now no longer guaranteed that replace(), toUpperCase(), etc will return the original string if there was no change. (But, strangely, this guarantee
is still there for trim().) While it's true that most common implementations do still seem to behave this way,
you should not rely on this behavior in your programs!