Can anyone explain why this produces an output of "not Equal" while instead of if(" String ".trim() == "String") , we replace if(" String" == "String") we get an output of "equal"... what happens internally?
Thanks in advance [ September 04, 2007: Message edited by: Radha Kamesh ]
== operator checks if both object references refer to the same memory location eg: if (a==b) it means it verifies whether both a and b refer to the same location in memory.
HI........ The functionality of "==" operator and equals() method is different.
1)."==":returns true if both the references are pointing to the same object. 2).equals():returns true if both the references point to objects that are equal in their context(Value).
So in your question trim() method is creating another object and is being applied to "==" operator which will return false.
when you apply equals() method it will return true.
So follows the output. .............................................................. Regards, Mack
String objects str3 and str4 are both created using string literals thus created in the String literal pool, and two objects created using the same literal will refer to the same object in the pool.