> Yes, but why was that decision made?
There are two approaches to the use of equals(Object). Unfortunately,
both are used from within the Java standard API.
The first approach says that equals(Object) should represent the concept
of two objects being the same. If a.equals(b) then it doesn't matter
whether I have a reference to 'a' or a reference to 'b'. This allows
equals(Object) to be overridden for String, Integer, Double, and any
other immutable value type. This approach would forbid implementing
equals(Object) to do comparison of contents for StringBuffer, because
even if two StringBuffer objects currently contain the same thing, I can
call append on one of them and it will be different from the other. Put
another way, it does matter whether I've got a reference to 'a' or 'b',
because someone might be modifying 'a'.
The second approach is that equals(Object) should be overridden to
implement whatever concept of equality seems most useful to the person
specifying a class or interface. In the standard API, this is seen for
java.util.List, which says that two lists are equal if they have the
same contents. This clearly violates the rule of the earlier approach,
since I can add elements to some Lists. The Collections API designer
just had a different idea about the proper use of equals(Object).
The first approach works better. For example, it is *not* safe to use
objects that have chosen this second approach as keys in a HashMap or
Hashtable. However, the second approach meshes better with some
people's intuition about what "equals" means, and it avoids the needs to
add other methods to do something like compare two Lists to see if they
have the same contents.

Signature
Chris Smith
Chris Uppal - 01 Sep 2006 16:27 GMT
> There are two approaches to the use of equals(Object). Unfortunately,
> both are used from within the Java standard API.
[quoted text clipped - 6 lines]
> implement whatever concept of equality seems most useful to the person
> specifying a class or interface.
Incidentally, I think the two approaches converge in this case. The name of
the class is StringBuffer (and later StringBuilder), not something like
MutableString. Instances have jobs to do, and that job is not representing
textual data, but knowing how to /assemble/ a String.
As such they are worker objects, and even when following the second approach
the "convenient equality" would tend to say that two instances were equal if
they were identically configured and were applying themselves to the same
target object. I can imagine builder objects for which those criteria would
allow a notion of equality that was weaker than identity, but I don't think
that's possible for StringBuilder.
(As an aside, I think the choice of toString() as the method to extract the
assembled String from the Builder was somewhat unfortunate since that isn't at
all what toString() /means/.)
-- chris