1. 程式人生 > >jdk1.8源碼閱讀(第2篇)java.lang.String

jdk1.8源碼閱讀(第2篇)java.lang.String

重要 tin () 分享 nds char cti 存儲 構造方法

重要屬性

/** The value is used for character storage. */ private final char value[];

*****存儲字符串的字符數組。該數組為final變量,一旦賦值,將不會更改。

/** Cache the hash code for the string */ private int hash; // Default to 0

*****該String對象的哈希值。

--------------------------------------------------------------------------------------------------------------

技術分享圖片
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
View Code

*序列化類的版本號

--------------------------------------------------------------------------------------------------------------

技術分享圖片
/**
 * Class String is special cased within the Serialization Stream Protocol.
 *
 * A String instance is written into an ObjectOutputStream according to
 * <a href="{@
docRoot}/../platform/serialization/spec/output.html"> * Object Serialization Specification, Section 6.2, "Stream Elements"</a> */ private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];
View Code

*返回的是字符串的序列化編號

--------------------------------------------------------------------------------------------------------------

構造方法

技術分享圖片
public String() {
    this.value = "".value;
}
View Code 技術分享圖片
public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}
View Code 技術分享圖片
public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}
View Code 技術分享圖片
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);
}
View Code 技術分享圖片
public String(int[] codePoints, int offset, int count) {
    if (offset < 0) {
        throw new StringIndexOutOfBoundsException(offset);
    }
    if (count <= 0) {
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        if (offset <= codePoints.length) {
            this.value = "".value;
            return;
        }
    }
    // Note: offset or count might be near -1>>>1.
    if (offset > codePoints.length - count) {
        throw new StringIndexOutOfBoundsException(offset + count);
    }

    final int end = offset + count;

    // Pass 1: Compute precise size of char[]
    int n = count;
    for (int i = offset; i < end; i++) {
        int c = codePoints[i];
        if (Character.isBmpCodePoint(c))
            continue;
        else if (Character.isValidCodePoint(c))
            n++;
        else throw new IllegalArgumentException(Integer.toString(c));
    }

    // Pass 2: Allocate and fill in char[]
    final char[] v = new char[n];

    for (int i = offset, j = 0; i < end; i++, j++) {
        int c = codePoints[i];
        if (Character.isBmpCodePoint(c))
            v[j] = (char)c;
        else
        Character.toSurrogates(c, v, j++);
    }

    this.value = v;
}
View Code 技術分享圖片
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) {
    checkBounds(ascii, offset, count);
    char value[] = new char[count];

    if (hibyte == 0) {
        for (int i = count; i-- > 0;) {
            value[i] = (char)(ascii[i + offset] & 0xff);
        }
    } else {
        hibyte <<= 8;
        for (int i = count; i-- > 0;) {
            value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));
        }
    }
    this.value = value;
}
View Code 技術分享圖片
@Deprecated
public String(byte ascii[], int hibyte) {
    this(ascii, hibyte, 0, ascii.length);
}
View Code 技術分享圖片
public String(byte bytes[], int offset, int length, String charsetName)
            throws UnsupportedEncodingException {
    if (charsetName == null)
        throw new NullPointerException("charsetName");
    checkBounds(bytes, offset, length);
    this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
View Code 技術分享圖片
public String(byte bytes[], int offset, int length, Charset charset) {
    if (charset == null)
            throw new NullPointerException("charset");
    checkBounds(bytes, offset, length);
    this.value =  StringCoding.decode(charset, bytes, offset, length);
}
View Code 技術分享圖片
public String(byte bytes[], String charsetName) throws UnsupportedEncodingException {
    this(bytes, 0, bytes.length, charsetName);
}
View Code 技術分享圖片
public String(byte bytes[], Charset charset) {
    this(bytes, 0, bytes.length, charset);
}
View Code 技術分享圖片
public String(byte bytes[], int offset, int length) {
    checkBounds(bytes, offset, length);
    this.value = StringCoding.decode(bytes, offset, length);
}
View Code 技術分享圖片
public String(byte bytes[]) {
    this(bytes, 0, bytes.length);
}
View Code 技術分享圖片
public String(StringBuffer buffer) {
    synchronized(buffer) {
        this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
    }
}
View Code 技術分享圖片
public String(StringBuilder builder) {
    this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
View Code 技術分享圖片
String(char[] value, boolean share) {
    // assert share : "unshared not supported";
    this.value = value;
}
View Code

jdk1.8源碼閱讀(第2篇)java.lang.String