站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题实现高效Java编程规范的十一条基础规则

实现高效Java编程规范的十一条基础规则

摘要: 本文介绍的Java规则的说明分为5个级别,级别1是最基本也是最重要的级别,在今后将陆续写出其他的规则。
  本文介绍的Java规则的说明分为5个级别,级别1是最基本也是最重要的级别,在今后将陆续写出其他的规则。遵守了这些规则可以提高程序的效率、使代码有更好的可读性等。

  (1) 避免使用NEW关键字来创建String对象

  把一个String常量copy到String 对象中通常是多余、浪费时间的。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>Public class test{
 Public void method(){
  System.out.print (str);
 }
 private String str = new String ("1"); //这里新建对象是完全没有必要的
 private String str2=”2” //正确的应该如此
}</td></tr></table>
  (2) 避免使用不必要的嵌套

  过多的嵌套会使你的代码复杂化,减弱可读性。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>Public class test {
 String add (){
  Int c=(a=a+b)+b; //过于复杂
  Return c
 }
}</td></tr></table>
  (3) 避免在同一行声明不同类型的多个变量

  这样可以使程序更加清晰,避免混乱

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>private int index, index1[];</td></tr></table>
  正确的应该如此:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>private int index;
private int index1[];</td></tr></table>
  (4) 在每一行里写一条语句

  这条规则不包括for语句:比如:'for (int i = 0; i < 10; i++) x--;’可以增加代码的可读性。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class OSPL {
 int method (int a, int b) {
  int i = a + b; return i; // 可读性不强
 }</td></tr></table>
  正确的:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class OSPLFixed {
 int method (int a, int b) {
  int i = a + b;
  return i;
 }
}</td></tr></table>
  (5)经常从finalize ()中调用super.finalize ()

  这里的finalize ()是java在进行垃圾收集的时候调用的,和finally不一样。如果你的父类没有定义finally()的话,你也应该调用。这里有两个原因:(1)在不改变代码的情况下能够将父类的finally方法加到你的类中。 (2)以后你会养成习惯调用父类的finally方法,即使父类没有定义finally方法的时候。

  正确的方法应该如此:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class parentFinalize {
 protected void finalize () throws Throwable {
  super.finalize(); // FIXED
 }</td></tr></table>
  (6) 不要在finalize ()中注销listeners

  不要再finalize ()方法中中注销listeners,finalize ()只有再没有对象引用的时候调用,如果listeners从finalize()方法中去除了,被finalize的对象将不会在垃圾收集中去除。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public void finalize () throws Throwable {
 bButton.removeActionListener (act);
}</td></tr></table>
  (7) 不要显式的调用finalize ()方法

  虽然显式的调用这个方法可以使你确保你的调用,但是当这个方法收集了以后垃圾收集会再收集一次。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class T7 {
 public void finalize() throws Throwable {
  close_resources ();
  super.finalize ();
 }
 public void close_resources() {}
}
class Test {
 void cleanup () throws Throwable {
  t71.finalize(); // 调用
  t71 = null;
 }
 private t71 = new T7 ();
}</td></tr></table>
  对于这样的调用我们应该自己创建一个释放的方法,做最初finalize ()所作的事情,当你每次想显式的调用finalize ()的时候实际上调用了释放方法。然后再使用一个判断字段来确保这个方法只执行一次,以后再调用就没关系了。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class T7 {
 public synchronized void release () throws Throwable{
  if (!_released) {
   close_resources (); // do what the old 'finalize ()'
   did _released = true;
  }
 }
 public void finalize () throws Throwable {
  release ();
  super.finalize ();
 }
 public void close_resources() {}
 private boolean _released = false;
}
class TestFixed {
 void closeTest () throws Throwable {
  t71 .release (); // FIXED
  t71 = null;
 }
 private T7 t71 = new T7 ();
}</td></tr></table>
  (8)不要使用不推荐的API

  尽量使用JDK1.3推荐的API。在类和方法或者java组件里有很多方法是陈旧的或者是可以选择的。有一些方法SUN用了"deprecated“标记。最好不要使用例如:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>private List t_list = new List ();
t_list.addItem (str); </td></tr></table>
  如果查一下javadoc的话,会发现建议用add()来代替addItem()。

  (9)为所有序列化的类创建一个'serialVersionUID'

  可以避免从你各种不同的类破坏序列的兼容性。如果你不特别制订一个UID的话,那么系统为自动产生一个UID(根据类的内容)。如果UID在你新版本的类中改变了,即使那个被序列化的类没改变,你也不能反序列化老的版本了。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class DUID implements java.io.Serializable { public void method () {}}</td></tr></table>
  在里面加一个UID,当这个类的序列化形式改变的时候,你也改变这个UID就可以了。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public class DUIDFixed implements java.io.Serializable {
 public void method () {}
 private static final long serialVersionUID = 1;
}</td></tr></table>
  (10)对于private常量的定义

  比较好的做法是对于这样的常量,加上final标记,这样的常量从初始化到最后结束值都不会改变。

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>private int size = 5; </td></tr></table>
  改变后的做法是:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>private final int size = 5; </td></tr></table>
  (11)避免把方法本地变量和参数定义成和类变量相同的名字

  这样容易引起混扰,建议把任何的变量字都定义成唯一的。这样看来,SCJP里的那些题目在现实中就用不到了:)

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public void method (int j) { final int i = 5; // VIOLATION } private int j = 2;</td></tr></table>
  建议:

<table borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1> <tr> <td>public void method (int j1) { final int i = 5; // VIOLATION } private int j = 2;</td></tr></table>

↑返回目录
前一篇: 用AJAX编写一个简单的相册
后一篇: AJAX:如何处理书签和后退按钮