Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flexibility to change a class's implementation once it has been released. If you accept the default serialized form, the class's private and package-priva…
Principles If you override the clone method in a nonfinal class, you should return an object obtained by invoking super.clone In practice, a class that implements Cloneable is expected to provide a properly functioning public clone method. Never make…
The choice of which overloading to invoke is made at compile time. // Broken! - What does this program print? public class CollectionClassifier { public static String classify(Set<?> s) { return "Set"; } public static String classify(List&…
Implementation theory The varargs facility works by first creating an array whose size is the number of arguments passed at the call site, then putting the argument values into the array, and finally passing the array to the method. // Simple use of…
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…
Chapter 11 Serialization Item 74: Implement Serializable judiciously 让一个类的实例可以被序列化不仅仅是在类的声明中加上"implements Serializable"那么简单. 当你的类implements Serializable并发布出去后,你对这个类的改动的灵活性将会大大降低,它的serialized form成为了它exported API的一部分,如果你不设计一个custom serialized for…
<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…
EFFECTIVE  JAVA  第十一章  系列化(将一个对象编码成一个字节流) 74.谨慎地实现Serializable接口 *实现Serializable接口付出的代价就是大大降低了“改变这个类的实现”的灵活性. *增加了出现Bug和安全漏洞的可能性.反系列化是一个“隐藏的构造器”,很难确保“由真正的构造器建立起来的关系” *增加了测试的负担 *transient关键字修饰的变量不会被初始化 *可以实现Externalizable接口来实现基于默认构造器的显示配置系列化  75.考虑使用自…
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用实例域,例如: 1 /** 2 * 枚举类型错误码 3 * Created by yulinfeng on 8/20/17. 4 */ 5 public enum ErrorCode { 6 FAILURE(0), 7 SUCCESS(1); 8 9 private final int code;…