您的位置:首页 > 编程语言 > Java开发

Java入门--深入讨论JAVA规则开发篇

2009-07-23 12:32 337 查看
本文介绍的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

(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/tutorial/i18n/intro/checklist.html

更多的字符比较方法请参考:http://java.sun.com/docs/books/tutorial/i18n/text/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/tutorial/i18n/intro/checklist.html

http://java.sun.com/docs/books/tutorial/i18n/format/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/tutorial/i18n/intro/checklist.html

http://java.sun.com/docs/books/tutorial/i18n/format/numberFormat.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/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

(10) 国际化开发建议:不要使用'StringTokenizer()'方法

错误示例:StringTokenizer st = new StringTokenizer(str);

可以从这里得到更多的信息:‘

参考:http://java.sun.com/docs/books/tutorial/i18n/intro/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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: