String源码阅读分享(一)

2018-08-22  本文已影响0人  IBuddha

String源码阅读分享(一)

查看String、StringBuffer与StringBuilder的关系

image

直接看String的源码

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

从类定义上可得:

  1. String类用final来修饰
  2. String类实现了Comparable、CharSequence接口

Comparable接口:

public interface Comparable<T> {
    public int compareTo(T o);
}
和当前对象进行比较

CharSequence接口:

public interface CharSequence {

    int length();

    char charAt(int index);

    CharSequence subSequence(int start, int end);

    public String toString();

    public default IntStream chars();

    public default IntStream codePoints();
}

String的构造器

    /** 使用的值存储 */
    private final char value[];

    /** hashCode存储 */
    private int hash; // Default to 0
    
    //空字符串
    public String() {
        this.value = "".value;
    }

    //字符串
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

    //分配一个字符串,后续修改char value[],不会修改原有的设置
    public String(char value[]); 

    //获取value的一部分
    public String(char value[], int offset, int count);

    //获取Unicode of Code Points的一部分
    //新的拷贝方式,详情分析
    public String(int[] codePoints, int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count <= 0) {
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            if (offset <= codePoints.length) {
                this.value = "".value;
                return;
            }
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > codePoints.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }

        final int end = offset + count;

        // Pass 1: Compute precise size of char[]
        int n = count;
        for (int i = offset; i < end; i++) {
            int c = codePoints[i];
            //
            if (Character.isBmpCodePoint(c))
                continue;
            else if (Character.isValidCodePoint(c))
                n++;
            else throw new IllegalArgumentException(Integer.toString(c));
        }

        // Pass 2: Allocate and fill in char[]
        final char[] v = new char[n];

        for (int i = offset, j = 0; i < end; i++, j++) {
            int c = codePoints[i];
            if (Character.isBmpCodePoint(c))
                v[j] = (char)c;
            else
                Character.toSurrogates(c, v, j++);
        }

        this.value = v;
    }
    
    //即将移除
    @Deprecated
    public String(byte ascii[], int hibyte, int offset, int count);

    //即将移除
    @Deprecated
    public String(byte ascii[], int hibyte) {
        this(ascii, hibyte, 0, ascii.length);
    }

    //以某个字符集解码解析,支持offset与length
    public String(byte bytes[], int offset, int length, String charsetName)
            throws UnsupportedEncodingException {
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        checkBounds(bytes, offset, length);
        this.value = StringCoding.decode(charsetName, bytes, offset, length);
    }

    //以某个charset解码解析
    public String(byte bytes[], int offset, int length, Charset charset) {
        if (charset == null)
            throw new NullPointerException("charset");
        checkBounds(bytes, offset, length);
        this.value =  StringCoding.decode(charset, bytes, offset, length);
    }

    //以某个字符集解码解析
    public String(byte bytes[], String charsetName)
            throws UnsupportedEncodingException {
        this(bytes, 0, bytes.length, charsetName);
    }

    //以某个charset解码
    public String(byte bytes[], Charset charset) {
        this(bytes, 0, bytes.length, charset);
    }

    public String(byte bytes[], int offset, int length) {
        checkBounds(bytes, offset, length);
        this.value = StringCoding.decode(bytes, offset, length);
    }

    public String(byte bytes[]) {
        this(bytes, 0, bytes.length);
    }

    public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }

    public String(StringBuilder builder) {
        this.value = Arrays.copyOf(builder.getValue(), builder.length());
    }

上一篇 下一篇

猜你喜欢

热点阅读