JDK源码学习笔记——String类
JDK版本为1.8.0_172
String类的源码解读
修饰符:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
String类是final修饰符修饰的,不可以被继承,是最终类。
实现的接口:
-
Serializable接口:Serializable接口是一个标记接口,用于实现序列化操作,如RMI的远程调用,IO流写入写出文件等。
-
comparable接口:该接口会对实现它的每个类进行对象的整体排序,String类实现这个接口主要是为了重写compareTo方法。
-
charSequence接口:实现这个接口可以提供length()、charAt()、chars()方法获取IntStream流等。
成员变量:
/** The value is used for character storage. */
//该值用于字符存储
/* 构造器传回来的字符串全部存在char[]数组中(1.9后会用bytep[]数组存储)
* 其加了final修饰符,拥有不可变特性
*/
private final char value[];
/** Cache the hash code for the string */
//存放字符串的哈希码
/*用来存放字符串经过hashcode()方法得到的hash值*/
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
//使用JDK 1.0.2中的serialVersionUID进行互操作
/*实现序列化的标识*/
private static final long serialVersionUID = -6849794470754667710L;
构造函数:
主要看这个5个函数
public String() {
this.value = "".value;
}
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
public String(byte bytes[], String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
}
注意:
当用String str = new String()
创建字符串时,字符串的值为 “”,而不是null。
重要函数:
equals()
方法
public boolean equals(Object anObject) {
//首先判断是否是同一个引用对象
if (this == anObject) {
return true;
}
//判断是否是String类型
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
//判断两者的长度是否相同
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
//不相同则直接返回true
return true;
}
}
return false;
}
hashcode()
方法
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
//hash值的计算过程,不同于Object类是直接使用本地方法根据存储地址产生的
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
charAt()
方法
//根据索引读取字符
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
compareTo()
方法
//compareTo就是比较两个值,如果前者大于后者,返回正数,等于返回0,小于返回负数
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
compareToIgnoreCase()
方法
//该方法与compareTo()一样,只是忽略了大小写
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
concat()
方法
/*如果参数字符串的长度为 0,则返回此 String 对象。否则,创建一个新的String对象,用来表示由此 String 对象表示的字符序列和参数字符串表示的字符序列连接而成的字符序列。*/
public String concat(String str) {
//传进来的字符串的长度
int otherLen = str.length();
//如果参数字符串的长度为 0,则返回此 String 对象
if (otherLen == 0) {
return this;
}
//原字符串的长度
int len = value.length;
//用来存放最终形式字符串的字符数组,通过Arrays.copyOf()来实现数组复制
char buf[] = Arrays.copyOf(value, len + otherLen);
//使用getChars()方法将新传进来的字符串拼接到原来的字符串中
str.getChars(buf, len);
//返回新的字符串对象
return new String(buf, true);
}
indexOf(String str)
和indexOf(String str, int fromIndex)
方法
public int indexOf(String str) {
return indexOf(str, 0);
}
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/*source--原字符串的字符数组
*sourceoffset--原字符串查找起始位置
*sourceCount--原字符串的长度
*target--目标字符串的字符数组
*targetoffset--目标字符串查找起始位置
*targetoCount--目标字符串的长度
*fromIndex--查询的起始位置
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
/*需要查找的第一个字符*/
char first = target[targetOffset];
/*最大查找索引值*/
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
/*查找第一个字符*/
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
/*注意 这里的下标i只是用来查找第一个字符,第二个字符开始用下标j*/
/*j--第二个字符开始匹配的下标
*end--匹配成功的标志位
*/
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
/*找到整个字符串*/
return i - sourceOffset;
}
}
}
return -1;
}
split(String regex)
和split(String regex, int limit)
方法
public String[] split(String regex) {
return split(regex, 0);
}
/*暂时没看懂,以后回来补注释*/
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
replace(char oldChar, char newChar)
方法
public String replace(char oldChar, char newChar) {
/*判断旧字符与新字符是否相同*/
if (oldChar != newChar) {
int len = value.length;
int i = -1;
/*将原字符串转为字符数组*/
char[] val = value; /* avoid getfield opcode */
/*找到旧字符并跳出循环*/
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
/*将所有的旧字符覆盖为新字符*/
if (i < len) {
char buf[] = new char[len];
/*把原字符串的字符数组赋值于buf[]数组*/
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
substring(int beginIndex)
和substring(int beginIndex, int endIndex)
方法
public String substring(int beginIndex) {
/*异常处理*/
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
/*subLen--截取后的长度*/
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
/*实际执行截取的代码*/
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
public String substring(int beginIndex, int endIndex) {
/*异常处理*/
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
/*subLen--截取后的长度*/
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
/*实际执行截取的代码*/
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}