本文介绍的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 ()' 方法。
这些方法在平时的开发或者是教科书里面也有用到过,但是这些方法会导致四锁的倾向。一下有充足的资料来说明为什么不建议用上述方法。
3.在表示长整常量的时候,用l来代替l,因为l很容易和1混一起。
错误示例:
long temp = 23434l;
正确示例:
long temp = 23434l;
4.最好在jsp开头写一条注释
在jsp文件头上面写一条注释,这样可以帮助别人来理解你的代码。这条规则不仅适用于jsp,更是用于任何开发的文档。
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;
}
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);
}
}
正确示例:
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 ());
}
}
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;
}
}
10.国际化开发建议:不要使用'stringtokenizer()'方法
错误示例:
stringtokenizer st = new stringtokenizer(str);
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);
}
}
闽公网安备 35060202000074号