String源码详解阅读
程序员文章站
2022-06-07 11:52:39
...
继承实现
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
//Serializable表明可序列化
//Comparable 比较器
//CharSequence 是一个描述字符串结构的接口
定义的变量
//通过char数组来保存,并被final修饰
private final char value[];
//hash是String实例化的hashcode的一个缓存
private int hash; // Default to 0
//序列化版本号
private static final long serialVersionUID = -6849794470754667710L;
构造函数
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);
}
。
。
。
。
。
。
String的构造函数太多了,本质上就是对value的赋值,也就是对char数组的操作。其中无非涉及到一些方法的调用,并没有特别复杂的逻辑。
常用方法
length
public int length() {
return value.length;
}
//本质就是数组的length
isEmpty
public boolean isEmpty() {
return value.length == 0;
}
//看数组的长度是否为0
charAt
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) { //如果index不合理,则抛出异常
throw new StringIndexOutOfBoundsException(index);
}
return value[index]; //操作数组,返回第index个字符
}
indexOf
public int indexOf(String str) {
return indexOf(str, 0);//实质调用了indexOf(String str, 0)
}
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
//调用indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int 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 */
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;
}
substring
//截取字符串
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
//上边都是判断index是否合理
//下边调用了判断之后 其中一种情况调用new String(value, beginIndex, subLen)
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
//如果间距小于数组的长度,返回空
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
// 调用 Arrays 类下的方法
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
完美的氵了一篇文章。good~~~