WEB开发网
开发学院WEB开发Jsp Java String 的 equals() 方法可能的优化 阅读

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

 2008-01-05 19:11:05 来源:WEB开发网   
核心提示:JDK1.4, 1.5 的 String Class 代码如下 以下内容为程序代码public final class String implements java.io.Serializable, Comparable<String>, CharSequence{ /** The value is u

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











以下内容为程序代码 以下内容为程序代码












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;
























 


 



 



以下内容为程序代码 以下内容为程序代码












   /**











   * 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;











   }























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

Tags:Java String equals

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接