Principle Use the Override annotation on every method declaration that you believe to override a superclass declaration. // Can you spot the bug? public class Bigram { private final char first; private final char second; public Bigram(char first, cha…
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 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可以给他加class能有的东西,比如constructor: public enum Planet { MERCURY(3.302e+23, 2.439e6), VENUS (4.869e+24, 6.052e6), EARTH (5.975e+24, 6.378e6): private final…
<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…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 40. 始终使用Override注解 Java类库包含几个注解类型.对于典型的程序员来说,最重要的是@Override.此注解只能在方法声明上使用,它表明带此注解的方法声明…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 36. 使用EnumSet替代位属性 如果枚举类型的元素主要用于集合中,一般来说使用int枚举模式(条目 34),下面将2的不同倍数赋值给每个常量: // Bit fiel…
Remove '@override' annotation解决办法      最近刚刚配置了新机器,将原来的代码放在eclipse上执行,总会出现Remove '@override' annotation,如果要一个个手动删除相当麻烦,最后在网上找了一下原因原来是编译器版本的问题.     @override:表示一个方法声明打算重写超类中的另一个方法声明.如果方法利用此注释类型进行注解但没有重写超类方法,则编译器会生成一条错误消息.     问题原因:Java 1.5的编译器默认对父类的方法进…
When it's the case that each instance of the class is equal to only itself. 1. Each instance of the class is inherently unique. 2. You don't care whether the class provides a "logical equality" test. 3. If a superclass has already overridden equ…
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;…