十四、對於boolean值,避免不必要的等式判斷
將一個boolean值與一個true比較是一個恆等操作(直接返回該boolean變量的值). 移走對於boolean的不必要操作至少會帶來2個好處:
1)代碼執行的更快 (生成的字節碼少了5個字節);
2)代碼也會更加乾淨 。
例子:
public class UEQ
{
boolean method (String string) {
return string.endsWith ("a") == true; // Violation
}
}
更正:
class UEQ_fixed
{
boolean method (String string) {
return string.endsWith ("a");
}
}
十五、對於常量字符串,用'String' 代替 'StringBuffer'
常量字符串並不需要動態改變長度。
例子:
public class USC {
String method () {
StringBuffer s = new StringBuffer ("Hello");
String t = s + "World!";
return t;
}
}
更正:
把StringBuffer換成String,如果確定這個String不會再變的話,這將會減少運行開銷提高性能。
十六、用'StringTokenizer' 代替 'indexOf()' 和'substring()'
字符串的分析在很多應用中都是常見的。使用indexOf()和substring()來分析字符串容易導致StringIndexOutOfBoundsException。而使用StringTokenizer類來分析字符串則會容易一些,效率也會高一些。
例子:
public class UST {
void parseString(String string) {
int index = 0;
while ((index = string.indexOf(".", index)) != -1) {
System.out.println (string.substring(index, string.length()));
}
}
}
參考資料:
Graig Larman, Rhett Guthrie: "Java 2 Performance and Idiom Guide"
Prentice Hall PTR, ISBN: 0-13-014260-3 pp. 282 – 283
十七、使用條件操作符替代"if (cond) return; else return;" 結構
條件操作符更加的簡捷
例子:
public class IF {
public int method(boolean isDone) {
if (isDone) {
return 0;
} else {
return 10;
}
}
}
更正:
public class IF {
public int method(boolean isDone) {
return (isDone ? 0 : 10);
}
}
十八、使用條件操作符代替"if (cond) a = b; else a = c;" 結構
例子:
public class IFAS {
void method(boolean isTrue) {
if (isTrue) {
_value = 0;
} else {
_value = 1;
}
}
private int _value = 0;
}
更正:
public class IFAS {
void method(boolean isTrue) {
_value = (isTrue ? 0 : 1); // compact expression.
}
private int _value = 0;
}
十九、不要在循環體中實例化變量
在循環體中實例化臨時變量將會增加內存消耗
例子:
import java.util.Vector;
public class LOOP {
void method (Vector v) {
for (int i=0;i < v.size();i++) {
Object o = new Object();
o = v.elementAt(i);
}
}
}
更正:
在循環體外定義變量,並反覆使用
import java.util.Vector;
public class LOOP {
void method (Vector v) {
Object o;
for (int i=0;i<v.size();i++) {
o = v.elementAt(i);
}
}
}
二十、確定 StringBuffer的容量
StringBuffer的構造器會創建一個默認大小(通常是16)的字符數組。在使用中,如果超出這個大小,就會重新分配內存,創建一個更大的數組,並將原先的數組複製過來,再丟棄舊的數組。在大多數情況下,你可以在創建StringBuffer的時候指定大小,這樣就避免了在容量不夠的時候自動增長,以提高性能。
例子:
public class RSBC {
void method () {
StringBuffer buffer = new StringBuffer(); // violation
buffer.append ("hello");
}
}
更正:
爲StringBuffer提供寢大小。
public class RSBC {
void method () {
StringBuffer buffer = new StringBuffer(MAX);
buffer.append ("hello");
}
private final int MAX = 100;
}
參考資料:
Dov Bulka, "Java Performance and Scalability Volume 1: Server-Side Programming
Techniques" Addison Wesley, ISBN: 0-201-70429-3 p.30 – 31
二十一、儘可能的使用棧變量
如果一個變量需要經常訪問,那麼你就需要考慮這個變量的作用域了。static? local?還是實例變量?訪問靜態變量和實例變量將會比訪問局部變量多耗費2-3個時鐘週期。
例子:
public class USV {
void getSum (int[] values) {
for (int i=0; i < value.length; i++) {
_sum += value[i]; // violation.
}
}
void getSum2 (int[] values) {
for (int i=0; i < value.length; i++) {
_staticSum += value[i];
}
}
private int _sum;
private static int _staticSum;
}
更正:
如果可能,請使用局部變量作爲你經常訪問的變量。
你可以按下面的方法來修改getSum()方法:
void getSum (int[] values) {
int sum = _sum; // temporary local variable.
for (int i=0; i < value.length; i++) {
sum += value[i];
}
_sum = sum;
}
參考資料:
Peter Haggar: "Practical Java - Programming Language Guide".
Addison Wesley, 2000, pp.122 – 125
二十二、不要總是使用取反操作符(!)
取反操作符(!)降低程序的可讀性,所以不要總是使用。
例子:
public class DUN {
boolean method (boolean a, boolean b) {
if (!a)
return !a;
else
return !b;
}
}
更正:
如果可能不要使用取反操作符(!)
二十三、與一個接口 進行instanceof操作
基於接口的設計通常是件好事,因爲它允許有不同的實現,而又保持靈活。只要可能,對一個對象進行instanceof操作,以判斷它是否某一接口要比是否某一個類要快。
例子:
public class INSOF {
private void method (Object o) {
if (o instanceof InterfaceBase) { } // better
if (o instanceof ClassBase) { } // worse.
}
}
class ClassBase {}
interface InterfaceBase {}