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>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. 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…