JDK源碼閱讀(1.8) —— String
阿新 • • 發佈:2019-03-06
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.View Code3 * 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 }
、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