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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Effective Java - Always override toString() method

5,284

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,284
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Effective Java - Item 9 <ul><ul><li>By </li></ul></ul><ul><ul><li>Ferdous Mahmud Shaon and </li></ul></ul><ul><ul><li>Hasan Shihab Uddin </li></ul></ul><ul><ul><li>Software Engineer, </li></ul></ul><ul><ul><li>Escenic Bangladesh. </li></ul></ul>
  • 2. Item 9 :: Always override toString() <ul><li>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. </li></ul><ul><li>It consists of the class name followed by an “at” sign (@) and the unsigned hexadecimal representation of the hash code, </li></ul><ul><li>For example, “PhoneNumber@163b91” </li></ul>
  • 3. Always override toString() - contd <ul><li>But the returned string should be: “a concise but informative representation that is easy for a person to read.” </li></ul><ul><li>“ PhoneNumber@163b91” is concise and easy to read, it is not very informative when compared to “(408) 867-5309”. </li></ul><ul><li>So, it is recommended that all subclasses of java.lang.Object should override this method. </li></ul>
  • 4. Always override toString() - contd <ul><li>providing a good toString implementation makes your class much more pleasant to use . </li></ul><ul><li>The toString method is automatically invoked when any object is passed to println method or the string concatenation operator (+). System.out.println(&quot;Failed to connect: &quot; + phoneNumber); </li></ul><ul><li>Without a good toString() implementation, the above output won’t be intelligible. </li></ul>
  • 5. Always override toString() - contd <ul><li>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. </li></ul><ul><li>But it is impractical if the object is large . </li></ul><ul><li>In that case, the toString method should return a summary such as “Manhattan white pages (1487536 listings)” </li></ul>
  • 6. Always override toString() - contd <ul><li>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 . </li></ul><ul><li>The advantage of specifying the format in the documentation is that it serves as a standard , unambiguous , human-readable representation of the object. </li></ul>
  • 7. Always override toString() - contd
  • 8. Always override toString() - contd <ul><li>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. </li></ul><ul><li>public final class PhoneNumber { </li></ul><ul><li>private final int areaCode; </li></ul><ul><li>private final int exchange; </li></ul><ul><li>private final int extension; </li></ul><ul><li>public PhoneNumber(int areaCode, int exchange, int extension) { </li></ul><ul><li>this.areaCode = areaCode; </li></ul><ul><li>this.exchange = exchange; </li></ul><ul><li>this.extention = extention; </li></ul><ul><li>} </li></ul>
  • 9. Always override toString() - contd <ul><li>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 . </li></ul><ul><li>If you want to change the format in the future release, you'll have to break the code. </li></ul><ul><li>So if format is not specified , you preserve the flexibility to add information or improve the format in a subsequent release. </li></ul><ul><li>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. </li></ul>
  • 10. Thank You

×