The contract of toString()
:
toString method returns a string that "textually represents" this object
The general purpose of toString()
is to provide a human-readable representation of the object. Some classes go further and specify the detailed format of toString()
, which is not generally advisable, and if so, then only for the "value objects" (date, time, number).
Unless specified in the class's javadoc, you should never rely on a specific format of the text returned by toString()
.
The general recommendation: if your class wants to introduce a specific persistent text representation, it should be provided by a separate method. Thus you are never unpleasantly surprised if the implementation of toString()
changes later.