Obey the general contract when overriding equals 先了解equals 和 == 的区别,如果不重写equals, equals比较的仅仅只是是否引用同一个对象,==更多的是比较基本数值.当我们创建一些对象的时候. 当对象是单例模式不需要进行 equals 重写. Reflexive: For any non-null reference values x,x.equals(x) must return true. Symmetric:For any…
String s = new String("stringette"); // Don't do this. This will create an object each time Vs String s = "stringette"; class Person { private final Date birthDate; // Other fields, methods, and constructor omitted /** * The starting a…
This chapter concerns creating and destorying objects : when and how to create them, when and how to avoid creating them, how to ensure they are destoryed in a timely manner, how to manage any cleanup actions that must precede their destruction.…
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st language I have chosen for this migration. It's a nice chance to read some great books like "Effective Java 2nd Edition" and share the note for what I…
<Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 读书笔记 第1章 介绍 (Introduction) 第2章 创建和销毁对象(Creating and Destroying Objects) 第1条 考虑用静态工厂方式替代构造器(Consider static factory m…
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating and Destroying Objects Consider static factory methods instead of constructors Consider a builder when faced with many constructor parameters Enforce the s…
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Objects Consider static factory methods instead of constructors (factory方法可以拥有名称,可以避免重复创建,比如单例模式) Consider a builder when faced with many constructor parameter…
Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几种情况不需要你override这个类的equals方法:这个类的每一个实例天生就是unique的,比如Thread:这个类的父类已经override了equals,并已经足够:这是个private或者package的class,你确定equals永远不会被调用,保险起见这时候你其实可以overrid…
<Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一个字一个字去推敲,研究.所以,书呆子们一般都很xx,在我眼里. 2016.07.24作 1,用静态方法替代构造器,下面是很好的例子: public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 11. 重写equals方法时同时也要重写hashcode方法 在每个类中,在重写 equals 方法的时侯,一定要重写 hashcode 方法.如果不这样做,你的类违反了…