构造方法1:根据一个String实例化一个String
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
构造方法2:根据字符数据实例化一个String
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
构造方法3:从形参char value[]取出count个元素(从索引offset(不包括offset)向后取)放在当前String对象的成员变量char数组中
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);
}
构造方法4:将StringBuffer转为String
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
构造方法:将StringBuilder转为String
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
其他构造方法:将基本数据类型的数组转为String