Since equals() is a method in Object, StringBuffer inherits it, but it appears it doesn't do anything with it (i.e., override it to make its own implementation). Is this correct? From the books I've seen, the StringBuffer class doesn't list equals() as one of its methods. Regards, SP
Yes Stephan u r right. The StringBuffer class does not override the equals method. So using equals method with a StringBuffer always returns "false" Aruna. [This message has been edited by Aru (edited September 21, 2000).]
Since StringBuffer doesn't override equals(Object) we get the base behavior 'if (this == obj)' in Object.equals(Object). StringBuffer.equals() CAN return true when you pass the same reference. Since StringBuffer.append() returns 'this', your code returns true. [CODE] StringBuffer b1 = new StringBuffer("abc"); StringBuffer b2 = new StringBuffer("abc"); if (b1.equals(b1)); // always true if (b1.equals(b2)); // always false b2 is not ref to b1 if (b1.equals(b1.append("def"))); // always true ref == ref b2 = b1; if (b1.equals(b2)); // now same ref so true!
Joined: Nov 22, 2008
well, it's good info i got that Stringbuffer doesn't provide 'equals' method and it returns TRUE only when both objects refer to same object. I didn't know that before!! thanks to all Ashish