Your SlideShare is downloading. ×
  • Like
Effective Java - Always override toString() method
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Effective Java - Always override toString() method



  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Effective Java - Item 9
      • By
      • Ferdous Mahmud Shaon and
      • Hasan Shihab Uddin
      • Software Engineer,
      • Escenic Bangladesh.
  • 2. Item 9 :: Always override toString()
    • While java.lang.Object provides an implementation of the toString() method, the string that it returns is generally not what the user of your class wants to see.
    • It consists of the class name followed by an “at” sign (@) and the unsigned hexadecimal representation of the hash code,
    • For example, “PhoneNumber@163b91”
  • 3. Always override toString() - contd
    • But the returned string should be: “a concise but informative representation that is easy for a person to read.”
    • “ PhoneNumber@163b91” is concise and easy to read, it is not very informative when compared to “(408) 867-5309”.
    • So, it is recommended that all subclasses of java.lang.Object should override this method.
  • 4. Always override toString() - contd
    • providing a good toString implementation makes your class much more pleasant to use .
    • The toString method is automatically invoked when any object is passed to println method or the string concatenation operator (+). System.out.println("Failed to connect: " + phoneNumber);
    • Without a good toString() implementation, the above output won’t be intelligible.
  • 5. Always override toString() - contd
    • The toString method should return all of the interesting information contained in the object, as in the phone number example just shown: area code, exchange, extension.
    • But it is impractical if the object is large .
    • In that case, the toString method should return a summary such as “Manhattan white pages (1487536 listings)”
  • 6. Always override toString() - contd
    • One important decision you'll have to make when implementing a toString method is whether to specify the format of the return value in the documentation .
    • The advantage of specifying the format in the documentation is that it serves as a standard , unambiguous , human-readable representation of the object.
  • 7. Always override toString() - contd
  • 8. Always override toString() - contd
    • If you specify the format, it's usually a good idea to provide a matching String constructor , so that programmers can easily translate back and forth between the object and its string representation.
    • public final class PhoneNumber {
    • private final int areaCode;
    • private final int exchange;
    • private final int extension;
    • public PhoneNumber(int areaCode, int exchange, int extension) {
    • this.areaCode = areaCode;
    • = exchange;
    • this.extention = extention;
    • }
  • 9. Always override toString() - contd
    • The disadvantage of specifying the format of the toString return value is that once you've specified it, you're stuck with it for life .
    • If you want to change the format in the future release, you'll have to break the code.
    • So if format is not specified , you preserve the flexibility to add information or improve the format in a subsequent release.
    • Whether or not you decide to specify the format, you should clearly document your intentions. If you specify the format, you should do so precisely.
  • 10. Thank You