7.10 The toString and equals Methods
In addition to constructors, mutator methods, and accessor methods, a well-designed class usually implements the toString and equals methods.
The toString method is called automatically when an object reference is used as a String. For example, the toString method for an object is called when the object reference is used with, or as, a parameter to System.out.println. The function of the toString method is to return a printable representation of the object data.
The equals method is designed to compare two objects for equality; that is, it typically returns true if the corresponding instance variables in both objects are equal in value. The equals method takes an Object reference parameter that is expected ...
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.