1. 程式人生 > >JDK源碼閱讀(1.8) —— String

JDK源碼閱讀(1.8) —— String

tor his splay string open substring use 常用 new

String類有兩個私有的變量,字符數組value,和整型變量hash(默認為0)。

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

下面對一些常用方法進行分析。

length()

技術分享圖片
 1  /**
 2      * Returns the length of this string.
3 * The length is equal to the number of <a href="Character.html#unicode">Unicode 4 * code units</a> in the string. 5 * 6 * @return the length of the sequence of characters represented by this 7 * object. 8 */ 9 public int length() {
10 return value.length; 11 }
View Code

、isEmpty()、charAt(int index)、equals(Object anObject)、hashCode()、indexOf(int ch)、substring(int beginIndex)、concat(String str)、

replace(char oldChar, char newChar) 、contains(CharSequence s)、

JDK源碼閱讀(1.8) —— String