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. Fiddler进行手机抓包

    测试设备:Windows8.1,Android 4.0.4山寨Android手机 网络环境:电信校园网,Wifi环境 测试工具:Fiddler4 完全参考了:http://www.jb51.net/s ...

  2. Jquery几个比较实用,但又让很多人忽略的几个函数

    工作中接触的人中,这些函数的使用频率比较少,我用的又比较好用的几个函数 来给大家分享一下. 你有你喜欢的,也可以分享一下 1.filter 使用了我要什么就有什么 这个函数不但可以很方便的筛选自定义H ...

  3. MyBatis动态SQL使用,传入参数Map中的Key判断

    <select id="" parameterType="Map" resultMap="commodityResultMap" &g ...

  4. 使用VS2010开发Qt程序的一点经验

    导读 相比于Qt Creator,我更喜欢用VS2010来进行开发.虽然启动时间相对较慢,但是VS下强大的快捷键和丰富的插件,以及使用多年的经验,都让我觉得在开发过程中得心应手.其中最重要的一点是,有 ...

  5. margin的使用方法与技巧

    1.margin还可以用来做平移,作用类似translate哈哈.将元素设成absolute后就可以用margin随便平移他了,既不像relative那样要霸占空间,又不用为父元素设置relative ...

  6. sencha treestore 取消自动加载数据

    gridstore在设置了autoLoad=false后不会自动加载数据,但是treestore不行,后来发现删掉root里的expanded:true后就可以了.但是界面上树没有展开,需在store ...

  7. 【jQuery基础学习】08 编写自定义jQuery插件

    目的:虽然jQuery各种各样的功能已经很完善了,但是我们还是要学会自己去编写插件.这样我们可以去封装一些项目中经常用到的专属的代码,以便后期维护和提高开发效率. jQuery插件的类型: 封装对象方 ...

  8. 【转】RBAC权限管理

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  9. ACdrea 1217---Cracking' RSA(高斯消元)

    ACdrea  1217---高斯消元 Description The following problem is somehow related to the final stage of many ...

  10. mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(摘录)

    mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法分析总结: 话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添 ...