[关闭]
@ZeroGeek 2015-08-27T06:41:47.000000Z 字数 2444 阅读 579

从Zero学习String源码

Java 基础知识


源码版本:JDK_1.7.0_55

String的常用方法

  1. public final class String //注意final
  2. implements java.io.Serializable, Comparable<String>, CharSequence {
  3. //保证只能初始化一次,用字符数组来存
  4. private final char value[];
  5. // 存储hashCode,默认为0
  6. private int hash;
  7. //判断是否相等
  8. public boolean equals(Object anObject) {
  9. if (this == anObject) { //同一对象
  10. return true;
  11. }
  12. if (anObject instanceof String) { //若同为String类型
  13. String anotherString = (String) anObject;
  14. int n = value.length; //本字符串长度
  15. if (n == anotherString.value.length) { //长度相同,比较每个字符(ASCII码)是否相同
  16. char v1[] = value;
  17. char v2[] = anotherString.value;
  18. int i = 0;
  19. while (n-- != 0) {
  20. if (v1[i] != v2[i])
  21. return false;
  22. i++;
  23. }
  24. return true; //相等返回
  25. }
  26. }
  27. return false;
  28. }
  29. //比较String的大小,返回的是 "str1 - str2"
  30. public int compareTo(String anotherString) {
  31. int len1 = value.length;
  32. int len2 = anotherString.value.length;
  33. int lim = Math.min(len1, len2); //得到小的长度(可以比较的最大长度)
  34. char v1[] = value;
  35. char v2[] = anotherString.value;
  36. int k = 0;
  37. while (k < lim) { //从0比较到lim-1
  38. char c1 = v1[k];
  39. char c2 = v2[k];
  40. if (c1 != c2) {
  41. return c1 - c2; //不相等返回
  42. }
  43. k++;
  44. }
  45. return len1 - len2; //若v[lim-1]以前都相等,则判断长度,长的值大
  46. }
  47. //获得hashCode,赋值给hash
  48. public int hashCode() {
  49. int h = hash;
  50. if (h == 0 && value.length > 0) {
  51. //若之前没有计算过hashCode,且存在String,则生成一个hashCode
  52. char val[] = value;
  53. for (int i = 0; i < value.length; i++) {
  54. h = 31 * h + val[i]; //计算方法,为了唯一性.
  55. }
  56. hash = h;
  57. }
  58. return h;
  59. }
  60. /*看了下"zero"生成hashCode过程:
  61. 0:122
  62. 1:3883
  63. 2:120487
  64. 3:3735208
  65. hashCode:3735208
  66. */
  67. //得到某一字符
  68. public char charAt(int index) {
  69. if ((index < 0) || (index >= value.length)) { //是否越界
  70. throw new StringIndexOutOfBoundsException(index);
  71. }
  72. return value[index]; //直接下标返回
  73. }
  74. //替换方法
  75. public String replace(char oldChar, char newChar) {
  76. if (oldChar != newChar) {
  77. int len = value.length;
  78. int i = -1;
  79. char[] val = value; /* avoid getfield opcode */
  80. while (++i < len) {
  81. if (val[i] == oldChar) { //遍历判断第一个oldChar的位置
  82. break;
  83. }
  84. }
  85. if (i < len) { //存在oldChar
  86. char buf[] = new char[len]; //重新生成等长字符数组
  87. for (int j = 0; j < i; j++) {
  88. buf[j] = val[j]; //0至i-1的值保持不变
  89. }
  90. while (i < len) {
  91. char c = val[i];
  92. buf[i] = (c == oldChar) ? newChar : c;
  93. //找到存在oldChar位置的地方,在新字符数组处赋值为newChar;
  94. i++;
  95. }
  96. return new String(buf, true); //实际上是重新生成了一个String
  97. }
  98. }
  99. return this;
  100. }
  101. //去掉两端的空格
  102. public String trim() {
  103. int len = value.length;
  104. int st = 0;
  105. char[] val = value; /* avoid getfield opcode */
  106. while ((st < len) && (val[st] <= ' ')) { //从前往后判断
  107. st++;
  108. }
  109. while ((st < len) && (val[len - 1] <= ' ')) { //后往前
  110. len--;
  111. }
  112. return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
  113. //substring(st, len),里面是用了Arrays里面的copyOfRange方法
  114. /*
  115. public static char[] copyOfRange(char[] original, int from, int to) {
  116. int newLength = to - from;
  117. if (newLength < 0)
  118. throw new IllegalArgumentException(from + " > " + to);
  119. char[] copy = new char[newLength];
  120. //native方法
  121. System.arraycopy(original, from, copy, 0,
  122. Math.min(original.length - from, newLength));
  123. return copy;
  124. }
  125. */
  126. }
  127. public native String intern();
  128. ...
  129. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注