Re: How equals method works in StringBuffer?



Jeffrey Schwab wrote:
Chris Smith wrote:
<swornavidhya_m@xxxxxxxxxxx> wrote:
Hai,
In my following code, the output i obtained is: false. Whereas
my expectation for output is true. I need ur suggestions and ideas.

StringBuffer's equals method returns true only when a StringBuffer object is compared with itself. It returns false when compared with any other StringBuffer, even if the two contain the same characters. This is actually quite a sensible behavior.

Chris, would you mind elaborating a little? I would have expected:

sb1.equal(sb2) == sb1.toString().equal(sb2.toString())

The API documentation for StringBuffer lists "equals" as a method
inherited from Object, so I expect the same behavior as the Object
equals method.

Maybe it was done that way because you can always write
sb1.toString().equals(sb2.toString()) if that is what you mean.

Patricia


.