会计考友 发表于 2012-8-4 12:44:44

JAVA认证:Java代码常见的十种错误分析

每一个程序员在编写代码的过程中都免不了出现错误或是小的失误,这些小的错误和失误往往使得程序员还得返工。那么,如何才能尽量避免这些错误的发生呢?笔者总结只有在日常的编写代码中总结出经验,在这篇文章中,笔者列出了10个Java编程中常见的错误,你可以把这些错误添加到你的代码审查的检查列表中,这样在经过代码审查后,你可以确信你的代码中不再存在这类错误了。
    一、常见错误1:多次拷贝字符串
    测试所不能发现的一个错误是生成不可变(immutable)对象的多份拷贝。不可变对象是不可改变的,因此不需要拷贝它。最常用的不可变对象是String。
    如果你必须改变一个String对象的内容,你应该使用StringBuffer。下面的代码会正常工作:
    String s = new String ("Text here");
    但是,这段代码性能差,而且没有必要这么复杂。你还可以用以下的方式来重写上面的代码:
    String temp = "Text here";  String s = new String (temp);
    但是这段代码包含额外的String,并非完全必要。更好的代码为:
    String s = "Text here";  二、常见错误2:没有克隆(clone)返回的对象
    封装(encapsulation)是面向对象编程的重要概念。不幸的是,Java为不小心打破封装提供了方便Java允许返回私有数据的引用(reference)。下面的代码揭示了这一点:
    import java.awt.Dimension;
    /** *//***Example class.The x and y values should never*be negative.*/
    public class Example…{
    private Dimension d = new Dimension (0, 0);
    public Example ()…{ }
    /** *//*** Set border="1" Height and width. Both border="1" Height and width must be nonnegative * or an exception is thrown.*/
    public synchronized void setValues (int border="1" Height,int width) throws IllegalArgumentException…{
    if (border="1" Height

会计考友 发表于 2012-8-4 12:44:45

JAVA认证:Java代码常见的十种错误分析

这段代码是安全的,但是就象在错误1#那样,又作了多余的工作。Integer对象,就象String对象那样,一旦被创建就是不可变的。因此,返回内部Integer对象,而不是它的拷贝,也是安全的。
    方法getValue()应该被写为:
    public synchronized Integer getValue()…{
    // "i" is immutable, so it is safe to return it instead of a copy.
    return i;  }
    Java程序比C++程序包含更多的不可变对象。JDK 所提供的若干不可变类包括:
    ·Boolean
    ·Byte
    ·Character
    ·Class
    ·Double
    ·Float
    ·Integer
    ·Long
    ·Short
    ·String  ·大部分的Exception的子类  四、常见错误4:自编代码来拷贝数组
    Java允许你克隆数组,但是开发者通常会错误地编写如下的代码,问题在于如下的循环用三行做的事情,如果采用Object的clone方法用一行就可以完成:
    public class Example…{
    private int[] copy;
    /** *//*** Save a copy of "data". "data" cannot be null.*/
    public void saveCopy (int[] data)…{
    copy = new int;
    for (int i = 0; i
    copy = data;
    }  }
    这段代码是正确的,但却不必要地复杂。saveCopy()的一个更好的实现是:
    void saveCopy (int[] data)…{
    try…{
    copy = (int[])data.clone();
    }catch (CloneNotSupportedException e)…{
    // Can"t get here.
    }  }
    如果你经常克隆数组,编写如下的一个工具方法会是个好主意:
    static int[] cloneArray (int[] data)…{
    try…{
    return(int[])data.clone();
    }catch(CloneNotSupportedException e)…{
    // Can"t get here.
    }  }
    这样的话,我们的saveCopy看起来就更简洁了:
    void saveCopy (int[] data)…{
    copy = cloneArray ( data);  }  五、常见错误5:拷贝错误的数据
    有时候程序员知道必须返回一个拷贝,但是却不小心拷贝了错误的数据。由于仅仅做了部分的数据拷贝工作,下面的代码与程序员的意图有偏差:
    import java.awt.Dimension;
    /** *//*** Example class. The border="1" Height and width values should never * be

会计考友 发表于 2012-8-4 12:44:46

JAVA认证:Java代码常见的十种错误分析

negative. */
    public class Example…{
    static final public int TOTAL_VALUES = 10;
    private Dimension[] d = new Dimension;
    public Example ()…{ }
    /** *//*** Set border="1" Height and width. Both border="1" Height and width must be nonnegative * or an exception will be thrown. */
    public synchronized void setValues (int index, int border="1" Height, int width) throws IllegalArgumentException…{
    if (border="1" Height
页: [1]
查看完整版本: JAVA认证:Java代码常见的十种错误分析