jdk1.8源码阅读(第2篇)java.lang.String

Posted danielmiau

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk1.8源码阅读(第2篇)java.lang.String相关的知识,希望对你有一定的参考价值。

重要属性

 /** 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的主要内容,如果未能解决你的问题,请参考以下文章

java基础系列之ConcurrentHashMap源码分析(基于jdk1.8)

JDK1.8源码——java.lang.String 类

JDK1.8源码——java.lang.String类

java.lang.Object底层代码分析-jdk1.8

JDK1.8源码分析02之阅读源码顺序

JDK1.8 ConcurrentHashMap源码阅读