[ July 03, 2008: Message edited by: Chiru Raj ] [ July 03, 2008: Message edited by: Chiru Raj ]
-Chiru
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
StringBuffer does not override the equals() method and so inherits the one in the Object class. Have a look at the documentation for Object.equals() and the output will make sense. [ July 03, 2008: Message edited by: Joanne Neal ]
Also, the reason the first comparison (s1==s2) returns false is that the == operator will, when used on objects, check if s1 and s2 are the same object. That is, if they are really references to the same memory area. Had you written like this:
Then s1==s2 would have been true.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35237
7
posted
0
All three lines testobject equality, not string equality. That's why they're mutually not equal.
Note that StringBuffer does not override the equals method, and thus uses the one inherited from Object (which tests for object equality). The String class, on the other hand, overrides equals to test for string equality.
Something like "s1.toString().equals(s2.toString())" might give you the result you were expecting.
PS: Which, as I now see, is just about what Joanne and Carl said. Oh well. [ July 03, 2008: Message edited by: Ulf Dittmer ]