|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
本文介绍的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.http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.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 (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; } } 参考: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html 更多的字符比较方式请参考:http://java.sun.com/docs/books/tutorial/i18n/text/charintro.html (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); } 参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html http://java.sun.com/docs/books/tutorial/i18n/format/dateFormat.html (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 ()); } } 参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html http://java.sun.com/docs/books/tutorial/i18n/format/numberFormat.html (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类的信息:http://java.sun.com/docs/books/tutorial/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; } } 参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html http://java.sun.com/docs/books/tutorial/i18n/text/locale.html (9) 国际化开发建议:不要使用´StringTokenizer()´方式 错误示例:StringTokenizer st = new StringTokenizer(str); 可以从这里得到更多的信息:‘ 参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html (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); } } 返回类别: 教程 上一教程: JAVAMAIL操作的总结(1) 下一教程: JSP+JAVASCRIPT实现类似MSDN CSDN导航树效果 您可以阅读与"JAVA规则 开发篇"相关的教程: · JAVA规则??开发篇 · Visual Studio:针对 Java 开发人员的 C# 编程语言 · JAVA程序开发小经验 - 使用ObjectStream会出现的问题 · 企业Java技术开发技巧2则 · java程序员:开发系统要注重的基本知识 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |