JDK中String類的源碼分析(一)


1、String類是final的,不允許被繼承

1     /** The value is used for character storage. */
2     private final char value[];
3 
4     /** Cache the hash code for the string */
5     private int hash; // Default to 0

String類的內部就是維護了一個char數組;

2、構造方法,只需要看兩個接受char數組的構造方法

 1 public String(char value[]) {
 2         this.value = Arrays.copyOf(value, value.length);
 3     }
 4 
 5     public String(char value[], int offset, int count) {
 6         if (offset < 0) {
 7             throw new StringIndexOutOfBoundsException(offset);
 8         }
 9         if (count < 0) {
10             throw new StringIndexOutOfBoundsException(count);
11         }
12         // Note: offset or count might be near -1>>>1.
13         if (offset > value.length - count) {
14             throw new StringIndexOutOfBoundsException(offset + count);
15         }
16         this.value = Arrays.copyOfRange(value, offset, offset+count);
17     }

這兩個構造方法都用到了,Arrays工具類的copyOf方法,在這兩個方法里面都調用了System.arraycopy方法;

因為System.arraycopy是一個系統本地方法,所以這個方法的效率很高,所以在構造String的時候效率也很高;

3、常用的length,charAt方法

通過第一條,很容易知道,這兩個方法,實際上就是在操作char數組

 1     public int length() {
 2         return value.length;
 3     }
 4 
 5     public boolean isEmpty() {
 6         return value.length == 0;
 7     }
 8 
 9     public char charAt(int index) {
10         if ((index < 0) || (index >= value.length)) {
11             throw new StringIndexOutOfBoundsException(index);
12         }
13         return value[index];
14     }

4、getBytes方法

調用了StringCoding.encode方法,encode方法調用另外一個重載的方法

 1     static byte[] encode(char[] ca, int off, int len) {
 2         String csn = Charset.defaultCharset().name();
 3         try {
 4             // use charset name encode() variant which provides caching.
 5             return encode(csn, ca, off, len);
 6         } catch (UnsupportedEncodingException x) {
 7             warnUnsupportedCharset(csn);
 8         }
 9         try {
10             return encode("ISO-8859-1", ca, off, len);
11         } catch (UnsupportedEncodingException x) {
12             // If this code is hit during VM initialization, MessageUtils is
13             // the only way we will be able to get any kind of error message.
14             MessageUtils.err("ISO-8859-1 charset not available: "
15                              + x.toString());
16             // If we can not find ISO-8859-1 (a required encoding) then things
17             // are seriously wrong with the installation.
18             System.exit(1);
19             return null;
20         }
21     }

得到一個字節數組,是由ISO-8859-1編碼得到,當然也可以用其他編碼

5、compareTo方法

因為String實現了Comparable接口,所、所以必須實現compareTo方法

 1     public int compareTo(String anotherString) {
 2         int len1 = value.length;
 3         int len2 = anotherString.value.length;
 4         int lim = Math.min(len1, len2);
 5         char v1[] = value;
 6         char v2[] = anotherString.value;
 7 
 8         int k = 0;
 9         while (k < lim) {
10             char c1 = v1[k];
11             char c2 = v2[k];
12             if (c1 != c2) {
13                 return c1 - c2;
14             }
15             k++;
16         }
17         return len1 - len2;
18     }

其實是遍歷的比較兩個數組

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM