111111
精灵王
精灵王
  • 注册日期2010-12-08
  • 发帖数640
  • QQ
  • 火币1103枚
  • 粉丝120
  • 关注75
  • 社区明星
阅读:4731回复:0

JAVA规则??研发篇-JSP教程,Java技巧及代码

楼主#
更多 发布于:2011-01-26 21:36
本文介绍的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);
}
}



喜欢0 评分0
游客

返回顶部