public final class String implements java.io.Serializable,Comparaable<String>,CharSequence{
private final char value[];
private int hash;
private static final long serialVersionUID=-6849794470754667710L;
private static final ObjectStreamField[] serialPersistentField=new ObjectStreamField();
如上图所示,String 类声明为final ,所以不能有子类。有个核心全局变量 char value[] 整个字符串的操作基本围绕其展开。
构造器提供了好多。如下图:
public String(){
this.value="".value;
}
public String(String original){
this.value=original.value;
this.hash=original.hash;
}
public String(char value[]){
this.value=Arrays.copyOf(value,value.length);
}
public String(char value[],int offset,int count){
if(offset<0){
throw new StringIndexOutBoundsException(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 -1>>>1
if(offset>value.length-count){
throw new StringIndexOutBoundsException(offset+count);
}
this.value=Arrays.copyOfRange(value,offset,offset+count);
}
只是列举了一部分。