value[]及对应的char类型数组 offset代表偏移量起始位置 count为偏移后的字符串长度 public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset);//当偏移量小于0时抛出异常 } if (count <= 0) { if (count < 0) { throw new StringIndexOutOfBoundsException(count);//抛出异常 } if (offset <= value.length) { this.value = "".value; //当偏移量小于字符串长度且count=0时,JVM会从常量池中找出一个空字符串或创建一个空字符串,然后将此字符串对象的value的引用传给this.value(参考知乎java中如何解释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);//正常返回 }
如有错误,请指正。