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. [linux]记录如何设置一个新的vps

    背景 我正在做一个小项目,做好了打算上线,所有需要买个服务器,看了一圈,发现还是卖个vps合算.买了之后,进行了一些列的设置,这里记录一下,以便后面查看. 系统: ubuntu 内存:1G 一.更改时 ...

  2. [mysql]brew 安装 配置 操作 mysql(中文问题)

    mac 下卸载mysqldmg mac下mysql的DMG格式安装内有安装文件,却没有卸载文件--很郁闷的事. 网上搜了一下,发现给的方法原来得手动去删. 很多文章记述要删的文件不完整,后来在stac ...

  3. Web 前端性能优化准则

    准则01:尽量减少http请求 “只有10%-20%的最终用户响应时间花在接收请求的HTML文档上,剩下的80%-90%时间花在HTML文档所引用的所有组件(图片,script,css,flash等等 ...

  4. AnyChatCoreSDK.h

    #ifndef ANYCHATCORESDK_H #define ANYCHATCORESDK_H #include <windows.h> #include <stdlib.h&g ...

  5. Ubuntu系统操作快捷键

    Ubuntu操作基本快捷键* 打开主菜单 = Alt + F1* 运行 = Alt + F2* 显示桌面 = Ctrl + Alt + d* 最小化当前窗口 = Alt + F9* 最大化当前窗口 = ...

  6. 【jQuery基础学习】11 jQuery性能简单优化

    关于性能优化 合适的选择器 $("#id")会直接调用底层方法,所以这是最快的.如果这样不能直接找到,也可以用find方法继续查找 $("p")标签选择器也是直 ...

  7. JPA学习(2)注解

    上一篇学习了JPA的helloworld,也初略的使用了一些注解,接下来就细细的了解一下有哪些注解,和这些注解的作用 JPA的基本注解: ①@Entity,@Table,@Id,@GeneratedV ...

  8. Python 学习笔记1

    1.Python2.x与3​​.x版本区别 2.常量与变量 3.if  elif  else 4.注释 5.用户交互 6.字符串拼接 7.文件扩展名 8.缩进 9.运算符 10.while循环 Pyt ...

  9. CentOS常用指令

    创建文件: 如touch a.txt 创建文件夹: mkdir -p 文件夹名,当文件夹不存在时候,创建这个文件夹 文件重命名: 把文件text.php得命名为index.php,可以是rename ...

  10. 非线性数据拟合-nls

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...