美文网首页
String源码阅读分享(一)

String源码阅读分享(一)

作者: IBuddha | 来源:发表于2018-08-22 23:36 被阅读0次

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

相关文章

  • String源码阅读分享(一)

    String源码阅读分享(一) 查看String、StringBuffer与StringBuilder的关系 直接...

  • 面试准备

    1. java基础 String源码阅读 String忽略大小写比较源码阅读 String的不变性 ArrayLi...

  • String源码阅读(一)

    概述 查阅String的源码时,首先需要阅读一下它的顶部注释,它里面说明了一些String在Java中的一些...

  • String源码阅读

    String源码阅读 wiki 通过反编译深入理解Java String及intern 成神之路-基础篇 Java...

  • String源码阅读

    String类是final类,且实现了java.io.Serializable,Comparable ,CharS...

  • String源码阅读

    1. 类的继承关系 2. 数据存储 private final char value[]; //String的字...

  • 第一天

    2022准备面试,阅读一些源码,提升自己. 一.String Stringbuffer Stringbuild ...

  • String源码阅读(二)

    构造方法 查阅String源码可以发现,它内部定义了很多构造方法,其中有些构造方法也都已经过时了,不建议使用...

  • String源码阅读(三)

    常用的几个方法 length() 它返回的是字符串的长度,length的数值其实是字符串中Unicode的code...

  • String类源码阅读

    1. 简介 String表示字符串,Java中所有字符串的字面值都是String类的实例,例如“ABC”。Stri...

网友评论

      本文标题:String源码阅读分享(一)

      本文链接:https://www.haomeiwen.com/subject/cjvuiftx.html