Advantage

Provide meaningful of an object info to client.

Disadvantage

Constrain the ability of changing the toString() implementation since this will affect the client who use this formatted string as persistence.

Note

  1. Whether or not you decide to specify the format, you should clearly document your intentions.

    /**

    * Returns the string representation of this phone number.

    * The string consists of fourteen characters whose format

    * is "(XXX) YYY-ZZZZ", where XXX is the area code, YYY is

    * the prefix, and ZZZZ is the line number. (Each of the

    * capital letters represents a single decimal digit.)

    *

    * If any of the three parts of this phone number is too small

    * to fill up its field, the field is padded with leading zeros.

    * For example, if the value of the line number is 123, the last

    * four characters of the string representation will be "0123".

    *

    * Note that there is a single space separating the closing

    * parenthesis after the area code from the first digit of the

    * prefix.

    */

    @Override public String toString() {

    return String.format("(%03d) %03d-%04d",

    areaCode, prefix, lineNumber);

    }

  2. Whether or not you specify the format, provide programmatic access to all of the information contained in the value returned by toString .

Effective Java 10 Always override toString() method的更多相关文章

  1. Java之所有对象的公用方法>10.Always override toString

    providing a good toString implementation makes your class much more pleasant to use. It is recommend ...

  2. Effective Java 09 Always override hashCode when you override equals

    Failure to do so will result in a violation of the general contract for Object.hashCode, which will ...

  3. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  4. Part 57 to 58 Why should you override ToString and Equal Method

    Part 57 Why should you override ToString Method sometimes you can override ToString method like that ...

  5. 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法

    Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...

  6. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. Effective Java 第三版——12. 始终重写 toString 方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java 第三版——40. 始终使用Override注解

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Effective Java 36 Consistently use the Override annotation

    Principle Use the Override annotation on every method declaration that you believe to override a sup ...

随机推荐

  1. [git]解决:git config --global push.default matching

    解决:git config --global push.default matching 这个警告的意思是:需要设置默认push的分支,所以设置好全局push的默认分支就好了.命令如下: 在有git目 ...

  2. 转载:第五弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!

    博卡君今天继续更新,忙了一天,终于有时间开工写教程.不罗嗦了,今天我们来看看如何实现一些前端的功能和效果. 第八章:微信小程序分组开发与左滑功能实现 先来看看今天的整体思路: 进入分组管理页面--&g ...

  3. 价值100W的经验分享: 基于JSPatch的iOS应用线上Bug的即时修复方案,附源码.

    限于iOS AppStore的审核机制,一些新的功能的添加或者bug的修复,想做些节日专属的活动等,几乎都是不太可能的.从已有的经验来看,也是有了一些比较常用的解决方案.本文先是会简单说明对比大部分方 ...

  4. 使用SignalR构建一个最基本的web聊天室

    What is SignalR ASP.NET SignalR is a new library for ASP.NET developers that simplifies the process ...

  5. Codeforces Round #313 (Div. 1) B. Equivalent Strings

    Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...

  6. Python好用的网站收集

    第三方Pthon包查找:http://www.lfd.uci.edu/ http://www.cnblogs.com/lanxuezaipiao/p/3543658.html

  7. wcf 双工

    服务器 [ServiceContract(Namespace="http://www.artech.com/", CallbackContract = typeof(ICallba ...

  8. CodeBlocks VS2015编译环境设置

    1. 菜单 Settings --> Compiler... 2. 设置vs 的安装路径

  9. hibernate映射文件one-to-one

    one-to-one 元素 属性: name:映射类属性的名字 class:映射的目标类 cascade:设置操作中的级联策略 可选值为 all所有操作情况均进行级联.none所有操作情况均不进行级联 ...

  10. window下从硬盘安装linux系统iso镜像文件的方法

    首先,需要安装grub2win,http://sourceforge.net/projects/grub2win/ 其次,将iso文件放在grub2可识别的分区, 如c:\abc\iso.iso 最后 ...