读书人

java 一些小技艺

发布时间: 2012-12-21 12:03:49 作者: rapoo

java 一些小技巧

1.在连接字符串的时候尽量避免使用String = "str"+"str2";
? 而使用StringBuffer str = new StringBuffer("str");str.append("str2")代替


2. 多使用PreparedStatement代替Statement这样可以避免在拼接字符串的时候出现
??? "select * from tablename where col = '"+col+"'"单引号过多的情况

?


3.在拼接查询语句的时候加上"where 1=1 "

?


4.多使用MessageFormat类


5.

代码中尽量少出现"123".equals(str)这样的危险字符

?而要 public static final String STR = "123"; STR.equals(str)去代替.

?


6.方法的命名要能够表达出方法的功能

?

?

7、为了程序的健壮性
我们可能使用Integer.ValueOf(1)去代替new Integer(1);

?

原因:public static Integer valueOf(int i)返回一个表示指定的 int 值的 Integer 实例。如果不需要新的 Integer 实例,则通常应优先使用该方法,而不是构造方法 Integer(int),因为该方法有可能通过缓存经常请求的值而显著提高空间和时间性能。

?

?

?

读书人网 >编程

热点推荐