Java String 源码解析

首先String类是final的(final 关键字用于类说明该类是不能被继承的)
类属性

private final char value[];//这是用于存储String字符的数组

 private final int offset;//这是value数组的第一个有效的字符的index

 private final int count;//这是String中的字符个数

 private int hash; // 存储String的hashcode,默认是0

下面开始介绍String的构造函数
public String()//默认的构造函数,因为String字符串是不可变的,所以没有必要使用
{
     offset = 0;
     count = 0;
     value = new char[0];
}
public String (String str){//使用已存在的字符串创建一个相同字符序列的字符串
                                    //Unless an explicit copy of original is needed, use of this constructor is
                                    //unnecessary
     //代码
     获取str的长度,int size = str.count;
     将str的字符序列拷贝到新的数组 char[] strValue = str.value;
     char[] v;
     if(strValue数组长度 > size){
          int off = str.offset;
          v = Arrays.copyOfRange(originalValue, off, off+size);
      }
      else{
           v = strValue ;
       }
      this.offset = 0;
      this.count = size;
      this.value = v;
}
 下面给出 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;
    }

下面是使用char字符数组来初始化String的构造函数
 public String(char value[]) {
        this.offset = 0;
        this.count = value.length;
        this.value = StringValue.from(value);
    }
在上面的代码中,有一个StringValue类:
class StringValue {
    private StringValue() { }

    /**
     * Returns a char array that is a copy of the given char array.
     */
    static char[] from(char[] value) {
        return Arrays.copyOf(value, value.length);
    }
}
通过这个类可以清楚地看到value是怎么从参数传过来的,easy

下面这个构造函数,是从value数组的子数组出发,构造一个新的String,在取得value的子数组的时候,要注意范围的判断(offset+count < value.length 才是合法的)
public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

public int length() {
        return count;
    }
public boolean isEmpty() {
        return count == 0;
    }

public char charAt(int index) {
        if ((index < 0) || (index >= count)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index + offset];
    }
trim删除一个字符串前面和后面的空白字符,
注意此时的判断条件不是== ‘ ’ 而是小于等于
public String trim() {
        int len = count;
        int st = 0;
        int off = offset; /* avoid getfield opcode */
        char[] val = value; /* avoid getfield opcode */

        while ((st < len) && (val[off + st] <= ' ')) {
            st++;
        }
        while ((st < len) && (val[off + len - 1] <= ' ')) {
            len--;
        }
        return ((st > 0) || (len < count)) ? substring(st, len) : this;
    }

1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意 思,说白了也就是字符串);
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值