本文介紹的JAVA規(guī)則的說明分為3個主要級別,本篇拋棄了平時開發(fā)中很少遇到的情況,那些用得比較少的以后再高級篇里面出現(xiàn)。并有六個有用的國際軟件開發(fā)重要注意的有關(guān)String的問題,遵守了這些規(guī)則可以提高程序的效率、使代碼又更好的可讀性等。
(1) 如果有JDBC連接沒有關(guān)掉的話,需要在"finally"方法中關(guān)掉
如果數(shù)據(jù)庫連接失敗或者是沒有釋放連接,看上去無關(guān)緊要。但是其他的用戶就需要用更長的時間等待連接,這樣數(shù)據(jù)庫利用效率就會下降。確保你的代碼在任何情況下,包括出錯或者程序異常終止的情況下都釋放數(shù)據(jù)庫連接。在"finally"方法中關(guān)掉連接,就可以確保這一點。
錯誤示例:
try {
Statement stmt = con.createStatement();
} catch(SQLException e) {
e.printStackTrace();
}
正確示例:
try {
Statement stmt = con.createStatement();
} finally {
if (con != null && !con.isClosed()) {
con.close();
}
}
ps:這個問題也是一直困擾一些提供jsp+mysql空間的網(wǎng)站,很多個人網(wǎng)站沒有關(guān)閉數(shù)據(jù)庫連接,結(jié)果造成整個服務器速度下降。
(2) 盡量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
這些方法在平時的開發(fā)或者是教科書里面也有用到過,但是這些方法會導致四鎖的傾向。一下有充足的資料來說明為什么不建議用上述方法。
參考:1."java.lang.Thread" in the JDK API documentation
2. http://java.sun.com/j2se/1.3/docs/g...eprecation.html
3.Paul Hyde: "Java Thread Programming"
Sams, ISBN: 0-672-31585-8 pp. 270
(3) 在表示長整常量的時候,用L來代替l.
因為l很容易和1混一起。
錯誤示例:
long temp = 23434l;
正確示例:
long temp = 23434L;
參考:Ken Arnold, James Gosling: "The Java Programming Language Second Edition"Addison Wesley, 1997, pp.108
(4) 最好在jsp開頭寫一條注釋
在 jsp文件頭上面寫一條注釋,這樣可以幫助別人來理解你的代碼。這條規(guī)則不僅適用于jsp,更是用于任何開發(fā)的文檔。
正確示例:<%-- JSP comment --%>
(5)明確的初始化一個構(gòu)造類里面的所有的字段
因為沒有初始化的字段會是一個潛在的bug,所以最好初始化類里面的所有的字段。特別是靜態(tài)的字段,最好在一開始就分配一個初始值
錯誤示例:
public class CSI {
public CSI () {
this (12);
k = 0;
}
public CSI (int val) {
j = val;
}
private int i = 5;
private int j;
private int k;
}
正確示例:
public class CSIFixed {
public CSIFixed () {
this (12);
}
public CSIFixed (int val) {
j = val;
k = 0;
}
private int i = 5;
private int j;
private int k;
}
參考:http://www.ambysoft.com/javaCodingStandards.pdf
(6) 國際化開發(fā)建議:邏輯操作符不要再一個單個的字符的前面或者后面
一個單個字符的前后不要用邏輯操作符,如果代碼要在一個國家環(huán)境中運行的話。我們可以使用字符比較方法,這些方法使用統(tǒng)一字符比較標準來定義字符的屬性的。
錯誤示例:public class CLO {
public boolean isLetter (char ch) {
boolean _isLetter = ( ch >= 'a' && ch <= 'z') //錯誤
|| (ch >= 'A' && ch <= 'Z');
return _isLetter;
}
}
正確示例:
public class CLOFixed {
public boolean isLetter (char ch) {
boolean _isLetter = Character.isLetter(ch);
return _isLetter;
}
}
參考: http://java.sun.com/docs/books/tuto.../checklist.html
更多的字符比較方法請參考:http://java.sun.com/docs/books/tuto.../charintro.html
(7) 國際化開發(fā)建議:不要對日期對象使用'Date.toString ()'
不要使用'Date.toString ()'方法,日期格式對于地區(qū)和語言不同的國家來說是不一樣的,務必不要使用。
錯誤示例:'DateFormat'類提供了一個預定義的格式類型來指定本地的格式。
public void printToday () {
Date today = new Date ();
String todayStr = today.toString ();
System.out.println (todayStr);
}
正確示例:
public void printToday () {
Locale currentLocale = Locale.getDefault ();
DateFormat dateFormatter = DateFormat.getDateInstance (
DateFormat.DEFAULT, currentLocale);
Date today = new Date ();
String todayStr = dateFormatter.format (today);
System.out.println (todayStr);
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...dateFormat.html
(8) 國際化開發(fā)建議:不要對數(shù)字變量使用'toString ()'方法
在全球化的開發(fā)中,不要對數(shù)字變量使用'toString ()'方法,對于java.lang.Number的任何子類都適用。包括:BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.對于這樣的情況,java里也與定義了"NumberFormat"方法來格式化。
錯誤示例:
public class NTS {
public void method (Double amount) {
String amountStr = amount.toString ();
System.out.println (amountStr);
}
}
正確示例:
public class NTSFixed {
public void method (Double amount) {
Locale currentLocale = Locale.getDefault ();
NumberFormat numberFormatter =
NumberFormat.getNumberInstance (currentLocale);
String amountStr = numberFormatter.format (amount); //
System.out.println (amountStr + ' ' + currentLocale.toString ());
}
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...mberFormat.html
(9) 國際化開發(fā)建議:不要使用'String.equals ()'方法
建議不要使用'String.equals ()'方法,因為在統(tǒng)一字符比較標準中不一定按照相關(guān)的順序來比較。'Collator'提供的預定義整理規(guī)則來排序string,Collator類調(diào)用'getInstance ()'方法,一般來說,可以為默認的本地創(chuàng)建一個Collator。例如:Collator myCollator = Collator.getInstance ();創(chuàng)建Collator的時候你也可以指定一個特殊的locale。例如:Collator myFrenchCollator = Collator.getInstance (Locale.FRENCH);然后就可以調(diào)用'Collator.compare ()'來執(zhí)行一個本地的字符比較myCollator.compare (s1,s2);從這里可以了解更多的有關(guān)Collator類的信息:http://java.sun.com/docs/books/tuto...ationintro.html
錯誤示例:
public class SE {
public boolean compstr (String s1, String s2) {
boolean b = (s1.equals (s2));
return b;
}
}
正確示例:
public class SEFixed {
public boolean compstr (String s1, String s2) {
Collator myCollator = Collator.getInstance ();
boolean b = (myCollator.compare(s1,s2) == 0);
return b;
}
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...ext/locale.html
(10) 國際化開發(fā)建議:不要使用'StringTokenizer()'方法
錯誤示例:StringTokenizer st = new StringTokenizer(str);
可以從這里得到更多的信息:‘
參考:http://java.sun.com/docs/books/tuto.../checklist.html
(11) 國際化開發(fā)建議:不要使用'Time.toString ()'方法
因為時間的格式各個國家也不一樣。如果你使用日期格式類,你的應用就能夠在世界上各個地方正確的顯示時間和日期了。首先,用'getTimeInstance ()'方法創(chuàng)建一個formatter。然后,調(diào)用'format ()'方法。
錯誤示例:
public class TTS {
public void printTime (Time t1) {
String timeStr = t1.toString ();
System.out.println (timeStr);
}
}
正確示例:
import java.sql.Time;
import java.text.DateFormat;
import java.util.Locale;
public class TTSFixed {
public void printTime (Time t1) {
DateFormat timeFormatter = DateFormat.getTimeInstance(
DateFormat.DEFAULT, Locale.getDefault ());
String timeStr = timeFormatter.format(t1);
System.out.println (timeStr);
}
}??
(1) 如果有JDBC連接沒有關(guān)掉的話,需要在"finally"方法中關(guān)掉
如果數(shù)據(jù)庫連接失敗或者是沒有釋放連接,看上去無關(guān)緊要。但是其他的用戶就需要用更長的時間等待連接,這樣數(shù)據(jù)庫利用效率就會下降。確保你的代碼在任何情況下,包括出錯或者程序異常終止的情況下都釋放數(shù)據(jù)庫連接。在"finally"方法中關(guān)掉連接,就可以確保這一點。
錯誤示例:
try {
Statement stmt = con.createStatement();
} catch(SQLException e) {
e.printStackTrace();
}
正確示例:
try {
Statement stmt = con.createStatement();
} finally {
if (con != null && !con.isClosed()) {
con.close();
}
}
ps:這個問題也是一直困擾一些提供jsp+mysql空間的網(wǎng)站,很多個人網(wǎng)站沒有關(guān)閉數(shù)據(jù)庫連接,結(jié)果造成整個服務器速度下降。
(2) 盡量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
這些方法在平時的開發(fā)或者是教科書里面也有用到過,但是這些方法會導致四鎖的傾向。一下有充足的資料來說明為什么不建議用上述方法。
參考:1."java.lang.Thread" in the JDK API documentation
2. http://java.sun.com/j2se/1.3/docs/g...eprecation.html
3.Paul Hyde: "Java Thread Programming"
Sams, ISBN: 0-672-31585-8 pp. 270
(3) 在表示長整常量的時候,用L來代替l.
因為l很容易和1混一起。
錯誤示例:
long temp = 23434l;
正確示例:
long temp = 23434L;
參考:Ken Arnold, James Gosling: "The Java Programming Language Second Edition"Addison Wesley, 1997, pp.108
(4) 最好在jsp開頭寫一條注釋
在 jsp文件頭上面寫一條注釋,這樣可以幫助別人來理解你的代碼。這條規(guī)則不僅適用于jsp,更是用于任何開發(fā)的文檔。
正確示例:<%-- JSP comment --%>
(5)明確的初始化一個構(gòu)造類里面的所有的字段
因為沒有初始化的字段會是一個潛在的bug,所以最好初始化類里面的所有的字段。特別是靜態(tài)的字段,最好在一開始就分配一個初始值
錯誤示例:
public class CSI {
public CSI () {
this (12);
k = 0;
}
public CSI (int val) {
j = val;
}
private int i = 5;
private int j;
private int k;
}
正確示例:
public class CSIFixed {
public CSIFixed () {
this (12);
}
public CSIFixed (int val) {
j = val;
k = 0;
}
private int i = 5;
private int j;
private int k;
}
參考:http://www.ambysoft.com/javaCodingStandards.pdf
(6) 國際化開發(fā)建議:邏輯操作符不要再一個單個的字符的前面或者后面
一個單個字符的前后不要用邏輯操作符,如果代碼要在一個國家環(huán)境中運行的話。我們可以使用字符比較方法,這些方法使用統(tǒng)一字符比較標準來定義字符的屬性的。
錯誤示例:public class CLO {
public boolean isLetter (char ch) {
boolean _isLetter = ( ch >= 'a' && ch <= 'z') //錯誤
|| (ch >= 'A' && ch <= 'Z');
return _isLetter;
}
}
正確示例:
public class CLOFixed {
public boolean isLetter (char ch) {
boolean _isLetter = Character.isLetter(ch);
return _isLetter;
}
}
參考: http://java.sun.com/docs/books/tuto.../checklist.html
更多的字符比較方法請參考:http://java.sun.com/docs/books/tuto.../charintro.html
(7) 國際化開發(fā)建議:不要對日期對象使用'Date.toString ()'
不要使用'Date.toString ()'方法,日期格式對于地區(qū)和語言不同的國家來說是不一樣的,務必不要使用。
錯誤示例:'DateFormat'類提供了一個預定義的格式類型來指定本地的格式。
public void printToday () {
Date today = new Date ();
String todayStr = today.toString ();
System.out.println (todayStr);
}
正確示例:
public void printToday () {
Locale currentLocale = Locale.getDefault ();
DateFormat dateFormatter = DateFormat.getDateInstance (
DateFormat.DEFAULT, currentLocale);
Date today = new Date ();
String todayStr = dateFormatter.format (today);
System.out.println (todayStr);
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...dateFormat.html
(8) 國際化開發(fā)建議:不要對數(shù)字變量使用'toString ()'方法
在全球化的開發(fā)中,不要對數(shù)字變量使用'toString ()'方法,對于java.lang.Number的任何子類都適用。包括:BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.對于這樣的情況,java里也與定義了"NumberFormat"方法來格式化。
錯誤示例:
public class NTS {
public void method (Double amount) {
String amountStr = amount.toString ();
System.out.println (amountStr);
}
}
正確示例:
public class NTSFixed {
public void method (Double amount) {
Locale currentLocale = Locale.getDefault ();
NumberFormat numberFormatter =
NumberFormat.getNumberInstance (currentLocale);
String amountStr = numberFormatter.format (amount); //
System.out.println (amountStr + ' ' + currentLocale.toString ());
}
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...mberFormat.html
(9) 國際化開發(fā)建議:不要使用'String.equals ()'方法
建議不要使用'String.equals ()'方法,因為在統(tǒng)一字符比較標準中不一定按照相關(guān)的順序來比較。'Collator'提供的預定義整理規(guī)則來排序string,Collator類調(diào)用'getInstance ()'方法,一般來說,可以為默認的本地創(chuàng)建一個Collator。例如:Collator myCollator = Collator.getInstance ();創(chuàng)建Collator的時候你也可以指定一個特殊的locale。例如:Collator myFrenchCollator = Collator.getInstance (Locale.FRENCH);然后就可以調(diào)用'Collator.compare ()'來執(zhí)行一個本地的字符比較myCollator.compare (s1,s2);從這里可以了解更多的有關(guān)Collator類的信息:http://java.sun.com/docs/books/tuto...ationintro.html
錯誤示例:
public class SE {
public boolean compstr (String s1, String s2) {
boolean b = (s1.equals (s2));
return b;
}
}
正確示例:
public class SEFixed {
public boolean compstr (String s1, String s2) {
Collator myCollator = Collator.getInstance ();
boolean b = (myCollator.compare(s1,s2) == 0);
return b;
}
}
參考:http://java.sun.com/docs/books/tuto.../checklist.html
http://java.sun.com/docs/books/tuto...ext/locale.html
(10) 國際化開發(fā)建議:不要使用'StringTokenizer()'方法
錯誤示例:StringTokenizer st = new StringTokenizer(str);
可以從這里得到更多的信息:‘
參考:http://java.sun.com/docs/books/tuto.../checklist.html
(11) 國際化開發(fā)建議:不要使用'Time.toString ()'方法
因為時間的格式各個國家也不一樣。如果你使用日期格式類,你的應用就能夠在世界上各個地方正確的顯示時間和日期了。首先,用'getTimeInstance ()'方法創(chuàng)建一個formatter。然后,調(diào)用'format ()'方法。
錯誤示例:
public class TTS {
public void printTime (Time t1) {
String timeStr = t1.toString ();
System.out.println (timeStr);
}
}
正確示例:
import java.sql.Time;
import java.text.DateFormat;
import java.util.Locale;
public class TTSFixed {
public void printTime (Time t1) {
DateFormat timeFormatter = DateFormat.getTimeInstance(
DateFormat.DEFAULT, Locale.getDefault ());
String timeStr = timeFormatter.format(t1);
System.out.println (timeStr);
}
}??