<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    心遠專欄

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      24 隨筆 :: 0 文章 :: 9 評論 :: 0 Trackbacks
    本文介紹的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();
    }
    }
    ps:這個問題也是一直困擾一些提供jsp+mysql空間的網站,很多個人網站沒有關閉數據庫連接,結果造成整個服務速度下降。


    (2) 盡量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
    這些方法在平時的開發或者是教科書里面也有用到過,但是這些方法會導致四鎖的傾向。一下有充足的資料來說明為什么不建議用上述方法。
    參考: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文件頭上面寫一條注釋,這樣可以幫助別人來理解你的代碼。這條規則不僅適用于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;
    }
    參考:http://www.ambysoft.com/javaCodingStandards.pdf

    (6) 國際化開發建議:邏輯操作符不要再一個單個的字符的前面或者后面
    一個單個字符的前后不要用邏輯操作符,如果代碼要在一個國家環境中運行的話。我們可以使用字符比較方法,這些方法使用統一字符比較標準來定義字符的屬性的。
    錯誤示例: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) 國際化開發建議:不要對日期對象使用'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);
    }
    參考:http://java.sun.com/docs/books/tuto.../checklist.html
    http://java.sun.com/docs/books/tuto...dateFormat.html

    (8) 國際化開發建議:不要對數字變量使用'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 ());
    }
    }
    參考:http://java.sun.com/docs/books/tuto.../checklist.html
    http://java.sun.com/docs/books/tuto...mberFormat.html

    (9) 國際化開發建議:不要使用'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類的信息: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) 國際化開發建議:不要使用'StringTokenizer()'方法
    錯誤示例:StringTokenizer st = new StringTokenizer(str);
    可以從這里得到更多的信息:‘
    參考:http://java.sun.com/docs/books/tuto.../checklist.html

    (11) 國際化開發建議:不要使用'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);
    }
    }
    ??
    posted on 2006-11-13 12:46 心遠 閱讀(152) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 最近2019免费中文字幕6| 一级成人a免费视频| 精品无码AV无码免费专区| 国产精品V亚洲精品V日韩精品| 久久亚洲精品无码gv| 日韩在线免费播放| 亚洲欧美日韩中文字幕在线一区 | 亚洲AV无码无限在线观看不卡| 99久久久国产精品免费牛牛| 亚洲综合国产精品| 精品免费久久久久久久| 亚洲一区二区三区久久| 免费毛片在线看片免费丝瓜视频 | 免费无码又爽又黄又刺激网站 | 免费的黄色网页在线免费观看| 免费在线观看a级毛片| 久99久无码精品视频免费播放| 色噜噜亚洲精品中文字幕| 无码免费一区二区三区免费播放 | 亚洲大片免费观看| 中文文字幕文字幕亚洲色| 国产精品免费视频一区| 美女被免费网站视频在线| 亚洲最大激情中文字幕| 16女性下面无遮挡免费| 亚洲色www永久网站| 亚洲成av人片不卡无码久久 | 在线视频免费观看高清| 亚洲AV色欲色欲WWW| 国产AV无码专区亚洲AV漫画| 久久99青青精品免费观看| 亚洲va在线va天堂成人| 久久亚洲中文字幕精品一区四| 嫩草在线视频www免费观看| 亚洲中文无码a∨在线观看| 国产一级一片免费播放i| 中出五十路免费视频| 亚洲第一精品电影网| 亚洲AV蜜桃永久无码精品| 99久久免费精品高清特色大片| 美女视频黄频a免费|