String 的源码大家应该都能看懂,这里就不一一分析咯,重点讲一下 equals()和 hashcode()方法,然后看一下 String 类常用方法的实现,就当一起温习一下咯。

    1. public final class String
    2. implements java.io.Serializable, Comparable<String>, CharSequence {
    3. /** 保存String的字节数组 */
    4. private final char value[];
    5. /** 缓存这个String的hash值 */
    6. private int hash; // Default to 0
    7. /** use serialVersionUID from JDK 1.0.2 for interoperability */
    8. private static final long serialVersionUID = -6849794470754667710L;
    9. /**
    10. * 1、Object的 hashCode()返回该对象的内存地址编号,而equals()比较的是内存地址是否相等;
    11. * 2、需要注意的是当equals()方法被重写时,hashCode()也要被重写;
    12. * 3、按照一般hashCode()方法的实现来说,equals()相等的两个对象,hashcode()必须保持相等;
    13. * equals()不相等的两个对象,hashcode()未必不相等
    14. * 4、一个类如果要作为 HashMap 的 key,必须重写equals()和hashCode()方法
    15. */
    16. public boolean equals(Object anObject) {
    17. if (this == anObject) {
    18. return true;
    19. }
    20. if (anObject instanceof String) {
    21. String anotherString = (String)anObject;
    22. int n = value.length;
    23. if (n == anotherString.value.length) {
    24. char v1[] = value;
    25. char v2[] = anotherString.value;
    26. int i = 0;
    27. while (n-- != 0) {
    28. if (v1[i] != v2[i])
    29. return false;
    30. i++;
    31. }
    32. return true;
    33. }
    34. }
    35. return false;
    36. }
    37. public int hashCode() {
    38. int h = hash;
    39. if (h == 0 && value.length > 0) {
    40. char val[] = value;
    41. for (int i = 0; i < value.length; i++) {
    42. h = 31 * h + val[i];
    43. }
    44. hash = h;
    45. }
    46. return h;
    47. }
    48. /**
    49. * 指定下标的char
    50. */
    51. public char charAt(int index) {
    52. if ((index < 0) || (index >= value.length)) {
    53. throw new StringIndexOutOfBoundsException(index);
    54. }
    55. return value[index];
    56. }
    57. /**
    58. * 是否以 prefix 为前缀
    59. */
    60. public boolean startsWith(String prefix) {
    61. return startsWith(prefix, 0);
    62. }
    63. /**
    64. * 是否以 suffix 为后缀
    65. */
    66. public boolean endsWith(String suffix) {
    67. return startsWith(suffix, value.length - suffix.value.length);
    68. }
    69. /**
    70. * 该String对象 是否满足 regex正则表达式
    71. */
    72. public boolean matches(String regex) {
    73. return Pattern.matches(regex, this);
    74. }
    75. /**
    76. * 字符替换
    77. */
    78. public String replace(char oldChar, char newChar) {
    79. if (oldChar != newChar) {
    80. int len = value.length;
    81. int i = -1;
    82. char[] val = value; /* avoid getfield opcode */
    83. while (++i < len) {
    84. if (val[i] == oldChar) {
    85. break;
    86. }
    87. }
    88. if (i < len) {
    89. char buf[] = new char[len];
    90. for (int j = 0; j < i; j++) {
    91. buf[j] = val[j];
    92. }
    93. while (i < len) {
    94. char c = val[i];
    95. buf[i] = (c == oldChar) ? newChar : c;
    96. i++;
    97. }
    98. return new String(buf, true);
    99. }
    100. }
    101. return this;
    102. }
    103. /**
    104. * 子串替换
    105. */
    106. public String replaceAll(String regex, String replacement) {
    107. return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    108. }
    109. /**
    110. * 子串替换,只替换第一个
    111. */
    112. public String replaceFirst(String regex, String replacement) {
    113. return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
    114. }
    115. /**
    116. * 按 regex 切割成多个子串
    117. */
    118. public String[] split(String regex) {
    119. return split(regex, 0);
    120. }
    121. /**
    122. * 剪切指定范围的字符串
    123. */
    124. public String substring(int beginIndex) {
    125. if (beginIndex < 0) {
    126. throw new StringIndexOutOfBoundsException(beginIndex);
    127. }
    128. int subLen = value.length - beginIndex;
    129. if (subLen < 0) {
    130. throw new StringIndexOutOfBoundsException(subLen);
    131. }
    132. return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    133. }
    134. public String substring(int beginIndex, int endIndex) {
    135. if (beginIndex < 0) {
    136. throw new StringIndexOutOfBoundsException(beginIndex);
    137. }
    138. if (endIndex > value.length) {
    139. throw new StringIndexOutOfBoundsException(endIndex);
    140. }
    141. int subLen = endIndex - beginIndex;
    142. if (subLen < 0) {
    143. throw new StringIndexOutOfBoundsException(subLen);
    144. }
    145. return ((beginIndex == 0) && (endIndex == value.length)) ? this
    146. : new String(value, beginIndex, subLen);
    147. }
    148. /**
    149. * 获取该String 对应的 char[]
    150. */
    151. public char[] toCharArray() {
    152. // Cannot use Arrays.copyOf because of class initialization order issues
    153. char result[] = new char[value.length];
    154. System.arraycopy(value, 0, result, 0, value.length);
    155. return result;
    156. }
    157. /**
    158. * 大小写转换
    159. */
    160. public String toLowerCase() {
    161. return toLowerCase(Locale.getDefault());
    162. }
    163. public String toUpperCase() {
    164. return toUpperCase(Locale.getDefault());
    165. }
    166. /**
    167. * str在本String对象中第一次出现的下标
    168. */
    169. public int indexOf(String str) {
    170. return indexOf(str, 0);
    171. }
    172. /**
    173. * str在本String对象中最后一次出现的下标
    174. */
    175. public int lastIndexOf(String str) {
    176. return lastIndexOf(str, value.length);
    177. }
    178. }