

细述Java技术开发规则--开发篇
<iframe id="ad_top" name="ad_top" align="left" marginwidth="0" marginheight="0" src="http://adv.pconline.com.cn/adpuba/show?id=pc.rjzx.kaifa.wenzhang.hzh.&media=html&pid=cs.pconline.rjzx.hzh." frameborder="0" width="320" scrolling="no" height="280">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();
}
}
long temp = 23434l;
long temp = 23434L;
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;
}
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;
}
}
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);
}
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);
}
}
本站地址:http://www.bajiao123.com

