String源码阅读分享(一)

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());
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Tip:笔者马上毕业了,准备开始 Java 的进阶学习计划。于是打算先从 String 类的源码分析入手,作为后面...
    石先阅读 12,057评论 16 58
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • 有人说婚姻就是搭伙过日子,找个适合的就行。 也有人说婚姻是爱情的坟墓,里面的人想出来、外面的人想进去。 更有人说婚...
    安然小语阅读 417评论 2 3
  • 另一半的确是来度我们的,这话在经历了几番深切的调到坑里的感受之后越发的认同。原来,理论的学习是为了指导我们在遇到真...
    hannah徐阅读 804评论 2 31
  • 太极的本性 徐怀清//创立篇 (一) 中华太极文化古已有之,是源远流长的。古人定义太极为:总天地万物为...
    焦作太极徐阅读 941评论 0 3