JAVA規則開發篇

本文介紹的JAVA規則的說明分爲3個主要級別,本篇拋棄了平時開發中不多遇到的狀況,那些用得比較少的之後再高級篇裏面出現。並有六個有用的國際軟件開發重要注意的有關String的問題,遵照了這些規則能夠提升程序的效率、使代碼又更好的可讀性等。      (1) 若是有JDBC鏈接沒有關掉的話,須要在"finally"方法中關掉    若是數據庫鏈接失敗或者是沒有釋放鏈接,看上去可有可無。可是其餘的用戶就須要用更長的時間等待鏈接,這樣數據庫利用效率就會降低。確保你的代碼在任何 狀況下,包括出錯或者程序異常終止的狀況下都釋放數據庫鏈接。在"finally"方法中關掉鏈接,就能夠確保這一點。   錯誤示例:   try {   Statement stmt = con.createStatement();   } catch(SQLException e) {   e.printStackTrace();   }   正確示例:   try {   Statement stmt = con.createStatement();   } finally {   if (con != null && !con.isClosed()) {   con.close();   }   }      (2) 儘可能避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。   這些方法在平時的開發或者是教科書裏面也有用到過,可是這些方法會致使四鎖的傾向。一下有充足的資料來講明爲何不建議用上述方法。   參考:1."java.lang.Thread" in the JDK API documentation   2.[url]http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.html[/url]   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文件頭上面寫一條註釋,這樣能夠幫助別人來理解你的代碼。這條規則不只適用於jsp,更是用於任何開發的文檔。   正確示例:<%-- JSP comment --%>      (5)明確的初始化一個構造類裏面的全部的字段   由於沒有初始化的字段會是一個潛在的bug,因此最好初始化類裏面的全部的字段。特別是靜態的字段,最好在一開始就分配一個初始值   錯誤示例:   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;   }   參考:[url]http://www.ambysoft.com/javaCodingStandards.pdf[/url]      (5) 國際化開發建議:邏輯操做符不要再一個單個的字符的前面或者後面   一個單個字符的先後不要用邏輯操做符,若是代碼要在一個國家環境中運行的話。咱們可使用字符比較方法,這些方法使用統一字符比較標準來定義字符的屬性的。   錯誤示例: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;   }   }   參考: [url]http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html[/url]   更多的字符比較方法請參考:[url]http://java.sun.com/docs/books/tutorial/i18n/text/charintro.html[/url]      (6) 國際化開發建議:不要對日期對象使用'Date.toString ()'   不要使用'Date.toString ()'方法,日期格式對於地區和語言不一樣的國家來講是不同的,務必不要使用。   錯誤示例:'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);   }   參考:[url]http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html[/url]   [url]http://java.sun.com/docs/books/tutorial/i18n/format/dateFormat.html[/url]      (7) 國際化開發建議:不要對數字變量使用'toString ()'方法    在全球化的開發中,不要對數字變量使用'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 ());   }   }   參考:[url]http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html[/url]   [url]http://java.sun.com/docs/books/tutorial/i18n/format/numberFormat.html[/url]      (8) 國際化開發建議:不要使用'String.equals ()'方法    建議不要使用'String.equals ()'方法,由於在統一字符比較標準中不必定按照相關的順序來比較。'Collator'提供的預約義整理規則來排序string,Collator類調 用'getInstance ()'方法,通常來講,能夠爲默認的本地建立一個Collator。例如:Collator myCollator = Collator.getInstance ();建立Collator的時候你也能夠指定一個特殊的locale。例如:Collator myFrenchCollator = Collator.getInstance (Locale.FRENCH);而後就能夠調用'Collator.compare ()'來執行一個本地的字符比較myCollator.compare (s1,s2);從這裏能夠了解更多的有關Collator類的信息:[url]http://java.sun.com/docs/books/tutorial[/url] /i18n/text/collationintro.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;   }   }      參考:[url]http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html[/url]   [url]http://java.sun.com/docs/books/tutorial/i18n/text/locale.html[/url]      (9) 國際化開發建議:不要使用'StringTokenizer()'方法   錯誤示例:StringTokenizer st = new StringTokenizer(str);   能夠從這裏獲得更多的信息:‘   參考:[url]http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html[/url]      (10) 國際化開發建議:不要使用'Time.toString ()'方法   由於時間的格式各個國家也不同。若是你使用日期格式類,你的應用就可以在世界上各個地方正確的顯示時間和日期了。首先,用'getTimeInstance ()'方法建立一個formatter。而後,調用'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); 要想看更多的技術文章推薦您看程式先鋒技術維客[url]www.javabiz.cn[/url]
相關文章
相關標籤/搜索