Even if I create a new class and try toString, I get a string like " PhoneNumber @ 163b91 " that I'm not happy to receive.
The general contract for toString is "a concise but informative expression that is easy for people to read."
/**
 *Returns a string representation of this phone number
 *The character string consists of 14 characters, and its format is"(XXX) YYY-ZZZZ"is.
 *XXX is the area code, YYY-ZZZZ is the city code.
 *(Each capital letter represents a one-digit number.)
 * 
 *Any of the three parts of this phone number can fill that field
 *If there are few digits, the field is padded with zeros.
 *For example, the last 4 digits of the number"123"If so, the last of the string representation
 *4 letters"0123"It will be.
 * 
 *Note that there is one space after the area code parentheses to separate it from the area code.
 */
@Override public String toString(){
    return String.format("(%03d) %03d-%04d", 
                         areaCode, prefix, lineNumber); 
}
This will return " {Jenny = (707) 867-5309} "!
Overriding toString exactly saves you the time of parsing another person's program using this class.
[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable" https://qiita.com/Natsukii/items/1942f7f41ac39b914591
Recommended Posts