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]
相关文章
相关标签/搜索