JDK源码阅读(2):String类阅读笔记

String

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence,
               Constable, ConstantDesc {
	...
}

1. value数组

用于存储String的字符。

@Stable
private final byte[] value;

@Stable注解表示变量最多被修改一次,称为“稳定的”。

2. checkBoundsOffCount和checkBoundsBeginEnd方法

String的很多构造方法使用了数组或者其他一些集合来创建新的String,这些构造方法大多含有以下参数

  • 集合S
  • 取值左端点p
  • 取值长度l

那么需要一个方法来检查这几个参数是否合法,所以String类提供了checkBoundsOffCount()方法

static void checkBoundsOffCount(int offset, int count, int length) {
    if (offset < 0 || count < 0 || offset > length - count) {
        throw new StringIndexOutOfBoundsException(
            "offset " + offset + ", count " + count + ", length " + length);
    }
}

如下是一个以bytes数组初始化String的例子,使用了上述方法。

public String(byte bytes[], int offset, int length, String charsetName)
            throws UnsupportedEncodingException {
    if (charsetName == null)
        throw new NullPointerException("charsetName");
    // 检查参数边界合法性
    checkBoundsOffCount(offset, length, bytes.length);
    StringCoding.Result ret =
        StringCoding.decode(charsetName, bytes, offset, length);
    this.value = ret.value;
    this.coder = ret.coder;
}

同时,还有一个checkBoundsBeginEnd()方法

static void checkBoundsBeginEnd(int begin, int end, int length) {
    if (begin < 0 || begin > end || end > length) {
        throw new StringIndexOutOfBoundsException(
            "begin " + begin + ", end " + end + ", length " + length);
    }
}

这个方法可以检查begin、end以及length是否适配

3. COMPACT_STRINGS常量

static final boolean COMPACT_STRINGS;

static {
    COMPACT_STRINGS = true;
}

这个常量用于表示是否支持字符串压缩,默认为true。

coder()方法内,要求返回String的编码,它是这样实现的。

byte coder(){
    return COMPACT_STRINGS ? coder : UTF16;
}

如果支持字符串压缩,就正常返回该字符串的coder,否则必须使用UTF16。

4. length方法

public int length() {
    return value.length >> coder();
}

获取value的长度,再根据字符集进行处理。

coder()的返回结果如LATIN1UTF16等都是字节常量。

@Native static final byte LATIN1 = 0;
@Native static final byte UTF16  = 1;

5. getChars方法

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)

这个方法可以将String中的一部分转化字符存到chars数组中。下面是一个简单的使用示例。

public static void main(String[] args) {
    String s = "abc";
    char[] cs = new char[50];
    s.getChars(0,2,cs,0);
    System.out.println(cs);
}

输出

ab

6. comepareTo方法

public int compareTo(String anotherString) {
    byte v1[] = value;
    byte v2[] = anotherString.value;
    byte coder = coder();
    if (coder == anotherString.coder()) {
        return coder == LATIN1 ? StringLatin1.compareTo(v1, v2)
            : StringUTF16.compareTo(v1, v2);
    }
    return coder == LATIN1 ? StringLatin1.compareToUTF16(v1, v2)
        : StringUTF16.compareToLatin1(v1, v2);
}

当调用String的compareTo方法时,会根据字符串的编码类型选择不同类的静态方法。而它们内部实现compare的算法都是类似的。下面给出一个例子。

// StringLating类
public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
    int lim = Math.min(len1, len2);
    for (int k = 0; k < lim; k++) {
        if (value[k] != other[k]) {
            return getChar(value, k) - getChar(other, k);
        }
    }
    return len1 - len2;
}

7. hashCode方法

String的哈希方法采用这个公式:\(s[0]*31^{(n-1)} + s[1]*31^{(n-2)} + \cdots + s[n-1]\)

public int hashCode() {
    int h = hash;
    if (h == 0 && !hashIsZero) {
        h = isLatin1() ? StringLatin1.hashCode(value)
            : StringUTF16.hashCode(value);
        if (h == 0) {
            hashIsZero = true;
        } else {
            hash = h;
        }
    }
    return h;
}

下面是StringLatin.hashCode()

public static int hashCode(byte[] value) {
    int h = 0;
    for (byte v : value) {
        h = 31 * h + (v & 0xff);
    }
    return h;
}

8. replaceAll和replaceFirst方法

public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

public String replaceFirst(String regex, String replacement) {
    // 用regex表示的正则表达式去匹配当前字符串,再调用Matcher.replace
    return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}

replaceAll()replaceFirst()方法接受regex参数和replacement参数。Pattern类从调用compile()方法解析正则表达式regex返回一个Mathcher,再调用它的replaceAll()replaceFirst()方法。

9. LATIN1和UTF16

String类中定义了LATIN1和UTF16两个编码格式

@Native static final byte LATIN1 = 0;
@Native static final byte UTF16  = 1;
  • ISO Latin-1是8比特的字符集,定义了256个字符。前128个字符(00000000-01111111)与ASCII完全一致。
    • 所以使用LATIN1作为编码格式时,往往要通过&0xff的方式取低八位
  • UTF-16也是采用可变长度编码,可以是一个或者两个16比特。
posted @ 2021-10-04 11:00  pedro7  阅读(408)  评论(1编辑  收藏  举报