八
用一個字符作為參數調用startsWith()也會工作的很好
例子
public class PCTS {
private void method(String s) {
if (s
//
}
}
}
更正
將
public class PCTS {
private void method(String s) {
if (
//
}
}
}
參考資料
Dov Bulka
Techniques
九
例子
public class SDIV {
public static final int NUM =
public void calculate(int a) {
int div = a /
int div
int temp = a /
}
}
更正
public class SDIV {
public static final int NUM =
public void calculate(int a) {
int div = a >>
int div
int temp = a /
}
}
十
同上
[i]但我個人認為
例子
public class SMUL {
public void calculate(int a) {
int mul = a *
int mul
int temp = a *
}
}
更正
package OPT;
public class SMUL {
public void calculate(int a) {
int mul = a <<
int mul
int temp = a *
}
}
十一
例子
public class STR {
public void method(String s) {
String string = s +
string =
}
}
更正
將一個字符的字符串替換成
public class STR {
public void method(String s) {
String string = s +
string =
}
}
十二
方法的同步需要消耗相當大的資料
例子
import java
public class SYN {
public synchronized void method (Object o) {
}
private void test () {
for (int i =
method (vector
}
}
private Vector vector = new Vector (
}
更正
不要在循環體中調用同步方法
import java
public class SYN {
public void method (Object o) {
}
private void test () {
synchronized{//在一個同步塊中執行非同步方法
for (int i =
method (vector
}
}
}
private Vector vector = new Vector (
}
十三
把try/catch塊放入循環體內
例子
import java
public class TRY {
void method (FileInputStream fis) {
for (int i =
try { // violation
_sum += fis
} catch (Exception e) {}
}
}
private int _sum;
}
更正
將try/catch塊移出循環
void method (FileInputStream fis) {
try {
for (int i =
_sum += fis
}
} catch (Exception e) {}
}
參考資料
Peter Haggar:
Addison Wesley
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26547.html