Synchronized classes Vector, Hashtable,Stack and StringBuffer should not be used
不要使用同步的Vector/HashTable/Stack/StringBuffer等。在早期,出于线程安全问题考虑,javaAPI 提供了这些类。但是同步会极大影响性能,即使是在同一个线程中使用他们。 通常可以这样取代:
ArrayList or LinkedList instead of Vector
Deque instead of Stack
HashMap instead of Hashtable
StringBuilder instead of StringBuffer
Exit methods should not be called
尽量不要调用system.exit()方法。
Local Variables should not be declared andthen immediately returned or thrown
本地变量如果赋值之后直接return了,那就直接return本地变量的赋值语句。
Field names should comply with a namingconvention
命名要规范
Local variable and method parameter namesshould comply with a naming convention
命名要规范
String literals should not be duplicated
字符串不应该重复,如果多次用到同一字符串,建议将该字符串定义为字符串常量,再引用。
Return of boolean expressions should not bewrapped into an "if-then-else" statement
不要写if ( a > 4 ) { return false } else { return true}这样的代码,直接写return a > 4。
Static non-final field names should complywith a naming convention
命名要规范
Modifiers should be declared in the correctorder
修饰符等要按约定俗成的顺序书写,例如,写成public static 而不是static public
The members of an interface declaration orclass should appear in a pre-defined order
与前面的一个问题类似,根据Oracle定义的Java代码规范中,不同代码的出现位置应该如下所示:
class and instance variables--Constructors--Methods
Array designators "[]" should beon the type, not the variable
数组的括号要写在类型后面,而不是变量后面,例如 int[] a 而不是int a[]
Multiple variables should not be declaredon the same line
不要在同一行定义多个变量
"switch" statements should haveat least 3 "case" clauses
当至少有3种或者3种以上的情况时,才考虑用switch,否则用if/else的形式。
Overriding methods should do more thansimply call the same method in the super class
既然在子类中重写了父类的某个方法,那就再这个方法中做些与父类方法不同的事情,否则没必要重写。
Statements should be on separate lines
不要把这样的代码写在同一行:if(someCondition) doSomething();而是应该写成下面的形式
if(someCondition) {
doSomething()
}