一: String类成员组成

String中的成员变量

private final char value[];//存放字符串
    private int hash; // Default to 0
    //使用JDK1.0.2中的serialVersionUID实现互操作性
    private static final long serialVersionUID = -6849794470754667710L;
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

字符串初始化的方式:3种类型的构造函数,直接定义在常量池中暂时不讨论

第一种: 无参构造方法

public String() {
        this.value = "".value;
    }

第二种: 传入一个字符串(直接在常量池中定义的一个字符串)

subString方法,这里传入hello word, String会把字符串转成数组存到成员变量value数组里面, hash值为0.

java string 源码阅读 java string源码解析_字符串

java string 源码阅读 java string源码解析_字符串_02


第三种: 传入一个数组, 此时他会进行数组拷贝。

java string 源码阅读 java string源码解析_数组_03

java string 源码阅读 java string源码解析_数组长度_04


第四种: 传入一个字符数组, 开始索引和数组长度, 在下面subString里面讲到。

第五种: 传入一个int数组, 开始索引和数组长度。这个的数字代表ASCII码, 源码比较长就不做展示。

java string 源码阅读 java string源码解析_数组_05


在往下面就是被 @Deprecated标记的构造函数。

还有一些这样的方法, 不是很懂这。。。

java string 源码阅读 java string源码解析_数组长度_06

二: subString的实现源码

java string 源码阅读 java string源码解析_数组_07

首先进行合法分析, 如果合法就调用new一个新的对象, 或者是自己this, 返回回去

//1
 public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }
    //2
        public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }

这里用到了一个String的第四种构造方法, 这里也是先进行一些合法性的分析, 如果合法, 就用Arrays.copyOfRange方法数组赋值

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);
        }
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

Arrays.copyOfRange的实现如下, 简单说, 就是把规定范围内的字符重新组成一个字符数组, 返回。

public static char[] copyOfRange(char[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));
        return copy;
    }

三:简单的一些方法

length: 返回数组的长度

java string 源码阅读 java string源码解析_字符串_08


isEmpty: 判断数组长度是不是0

java string 源码阅读 java string源码解析_数组长度_09


charAt: 直接返回数组指定下标的字符

java string 源码阅读 java string源码解析_数组长度_10


codePointAt返回指定下标的字符的ASCII码

java string 源码阅读 java string源码解析_java string 源码阅读_11


public boolean equals(Object obj), 先判断对象类型是不是String, 然后把对象转换成String类型, while循环遍历数组,比较字符

java string 源码阅读 java string源码解析_字符串_12


equalsIgnoreCase(String anotherString) : 先进进简单的判断, 是不是相等,如果不相等, 再进行忽略大小写的判断。调用regionMatches方法

java string 源码阅读 java string源码解析_字符串_13

regionMatches : 输入为: 是否忽略大小写字符串1开始比较位置字符串2开始比较位置比较长度

java string 源码阅读 java string源码解析_数组长度_14


split方法:第一个参数是分割字符,第二个是返回的数组的数量。0代表不限制数量, 第二种split进去会调用第一种, 默认传一个0limit进去

java string 源码阅读 java string源码解析_数组_15


java string 源码阅读 java string源码解析_java string 源码阅读_16

public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }