L
Lew
Lew said:Arved said:From the Object.toString() Javadocs:
"In general, the toString method returns a string that "textually
represents" this object."
[ embedded quotation in the original ]
To me that supports what Lew said, that toString() ought to be
_consistent_ with equals() and hashCode(). I can't think of a "textual
representation" of an object that ignores the information that
identifies it.
Precisely so.
Daniel said:Perhaps a "Timing" object, which has a toString() which returns time in
ms/s/minutes/etc..., but is not a value type (eg, identity is important).
But then you'd likely want the object ID of the particular 'Timing' object in
order to tell its timing information apart from that of other 'Timing' object,
wouldn't you?
The object is textually represented, but equals/hashCode are not
relevant to the contents of the textual representation.
Yes, they are, on the usual need to know which 'Timing' object you are
representing by 'toString()'.
You might pick something arbitrary not exactly the same as the object ID, but
what you pick had durned well better be consistent with the ID.
I can think of corner cases where 'toString()' wouldn't need to identify the
object it represents, but they are tortuous at best and violate the spirit of
'toString()': to provide a textual representation of the object, i.e., a
string that identifies the object. If that is the purpose of 'toString()',
and to a first order of approximation that is the purpose of 'toString()',
then the text must be consistent with the characteristics of the object that
identify it.
Violate at your peril.
The novice doesn't know the rules. The professional knows the rules by heart.
The virtuoso knows when to break the rules. The master creates the rules.