网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

Java String 的 equals() 方法可能的优化

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】






JDK1.4, 1.5 的 String Class 代码如下











[code]











public final class String











    implements java.io.Serializable, Comparable<String>, CharSequence











{











    /** The value is used for character storage. */











    private final char value[];












 


 



 



    /** The offset is the first index of the storage that is used. */











    private final int offset;












 


 



 



    /** The count is the number of characters in the String. */











    private final int count;











[/code]












 


 



 



[code]











    /**











     * Initializes a newly created <code>String</code> object so that it











     * represents the same sequence of characters as the argument; in other











     * words, the newly created string is a copy of the argument string. Unless











     * an explicit copy of <code>original</code> is needed, use of this











     * constructor is unnecessary since Strings are immutable.











     *











     * @param   original   a <code>String</code>.











     */











    public String(String original) {











            int size = original.count;











            char[] originalValue = original.value;











            char[] v;











            if (originalValue.length > size) {











                // The array representing the String is bigger than the new











                // String itself.  Perhaps this constructor is being called











                // in order to trim the baggage, so make a copy of the array.











                v = new char[size];











                System.arraycopy(originalValue, original.offset, v, 0, size);











            } else {











                // The array representing the String is the same











                // size as the String, so no point in making a copy.











                v = originalValue;











            }











            this.offset = 0;











            this.count = size;











            this.value = v;











    }











[/code]











从这段构造函数中,我们可以看出,不同Reference的String之间有可能共享相同的 char[]。












 


 



 



[code]











    /**











     * Compares this string to the specified object.











     * The result is <code>true</code> if and only if the argument is not











     * <code>null</code> and is a <code>String</code> obj

欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言