Exception translation: higher layers should catch lower-level exceptions and, in their place, throw exceptions that can be explained in terms of the higher-level abstraction. // Exception Translation try { // Use lower-level abstraction to do our bid…
Principle Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow. // Horrible abuse of exceptions. Don't ever do this! try { int i = 0; while(true) range[i++].climb(); }…
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 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用exception来控制control flow的终止,比如: // Horrible abuse of exceptions. Don't ever do this! try { int i = 0; while(true) range[i++].climb(); } catch(ArrayIndexOutO…
<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通俗理解(上) 第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;…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 29. 优先考虑泛型 参数化声明并使用JDK提供的泛型类型和方法通常不会太困难. 但编写自己的泛型类型有点困难,但值得努力学习. 考虑条目 7中的简单堆栈实现: // Ob…
本文参考 本篇文章参考自<Effective Java>第三版第九条"Prefer try-with-resources to try-finally" The code in both the try block and the finally block is capable of throwing exceptions.The second exception(in finally block) can completely obliterate the first…
Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类(HashMap,HashSet等)将不能正常工作. 8.覆盖equals时请遵守通用约定 无需覆盖equals的情形: 类的每个实例本质上是唯一的.类代表的是活动实体而不是值的概念.(例如,类Thread) 不关心类"逻辑相等"的功能,从Object继承的equals实现已经足够.(例如,…