hi Tom correct me if im wrong i think if an object is created at compile time then it goes into the string pool and if the object is created at runtime a new object is created that is the reason why when a String literal is used in '+' it creates one in pool instead of creatinga new object. and if u use a function it tries to return the same string if it can for example when we call concat with an empty string and trim() without empty spaces. Thank u for compiling the code Cherry
Joined: Dec 24, 2000
You are absolutely right. When you say s1==s1.trim(); it returns true. The compiler just knows that you are doing nothing.
from Jane: When you create a String using new or when the String is the result of a runtime operation Java creates a new object. If the String is a literal; the result is placed in a 'string pool'. If the the String already exists in the pool, a reference to the pre-existing string is returned by the compiler.