JDK源码之lang.String(一)String的成员变量及构造方法

今天开始看JDK源码的lang包下的String类。

可以看出String类实现了三个接口,且分别要实现他们的方法。

看一下String类中的成员变量:

 /** The value is used for character storage. */
//value属性是用来以数组的形式存储字符的
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    //偏移量是用来存储字符串的第一个字符的索引
    private final int offset;

    /** The count is the number of characters in the String. */
    //count表示字符串中字符的个数	
    private final int count;

    /** Cache the hash code for the string */
    //缓存字符串的哈希码
    private int hash; 

    /**  Default to 0 use serialVersionUID from JDK 1.0.2 for interoperability */
    
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * Class String is special cased within the Serialization Stream Protocol.
     *
     * A String instance is written initially into an ObjectOutputStream in the
     * following format:
     * <pre>
     *      <code>TC_STRING</code> (utf String)
     * </pre>
     * The String is written by method <code>DataOutput.writeUTF</code>.
     * A new handle is generated to  refer to all future references to the
     * string instance within the stream.
     */
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

 

String底层还是用数组来存储数据的,hash值是当使用了hashTable或者hashMap数据结构时,才会使用。后面两个变量是关于序列化的。

接下来看String类的构造函数:

     1. 无参构造器:

//初始化一个新创建的 String对象,使其表示一个空字符序列。注意,使用这个构造函数是不必要的,因为字符串是不可变的。
    public String() {
	this.offset = 0;
	this.count = 0;
	this.value = new char[0];
    }

3.有参构造器:

 public String(String original) {
	int size = original.count;
	char[] originalValue = original.value;
	char[] v;
  	if (originalValue.length > size) {
 	    // The array representing the String is bigger than the new
 	    // String itself.  Perhaps this constructor is being called
 	    // in order to trim the baggage, so make a copy of the array.
            int off = original.offset;
            v = Arrays.copyOfRange(originalValue, off, off+size);
 	} else {
 	    // The array representing the String is the same
 	    // size as the String, so no point in making a copy.
	    v = originalValue;
 	}
	this.offset = 0;
	this.count = size;
	this.value = v;
    }

originalValue.length > size字符串的数组比新字符串大。字符串本身。也许调用这个构造函数是为了减少负担,所以要复制数组。(为什么会出现这种场景还没有想明白)

4.通过字符数组参数来创建一个新的字符串

public String(char value[]) {
	int size = value.length;
	this.offset = 0;
	this.count = size;
	this.value = Arrays.copyOf(value, size);
    }

分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串

5.通过字符数组参数来创建一个新的字符串(新生成的字符串是根据字符数组的子数组创建的)

public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

分配一个新的字符串,该字符串包含字符数组参数的子数组中的字符。

offset参数是子数组第一个字符的索引,

count参数指定子数组的长度。

复制子数组的内容;字符数组的后续修改不影响新创建的字符串。

 

6.

 public String(int[] codePoints, int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // 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;
    }

 

分配一个新的字符串,该字符串包含Unicode代码点数组参数的子数组中的字符。

offset参数是子数组第一个代码点的索引,

count参数指定子数组的长度。

将子数组的内容转换为char;int数组的后续修改不会影响新创建的字符串。

 

 

7.

   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);
    }

通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。新 String 的长度是一个字符集函数,因此可能不等于子数组的长度。当给定 byte 在给定字符集中无效的情况下,此构造方法的行为没有指定。如果需要对解码过程进行更多控制,则应该使用 CharsetDecoder 类。

参数:

bytes - 要解码为字符的 byte

offset - 要解码的第一个 byte 的索引

length - 要解码的 byte 数

charsetName - 受支持 charset 的名称

 

8.

    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);
    }

 

通过使用指定的{@linkplain java.nio.charset解码指定的字节子数组,构造一个新的字符串。字符集字符集}。新的字符串的长度是字符集的一个函数,因此可能不等于子数组的长度。这个方法总是用这个字符集的默认替换字符串替换malformed-input和unmapping -character序列。的falink java.nio.charset。当需要对解码过程进行更多控制时,应该使用CharsetDecoder}类。

     * @param  bytes        :要解码成字符的字节
     * @param  offset        :要解码的第一个字节的索引
     * @param  length       :要解码的字节数
     * @param  charset     : java.nio.charset.Charset charset用于解码字节

9.

public String(byte bytes[], String charsetName)
	throws UnsupportedEncodingException
    {
	this(bytes, 0, bytes.length, charsetName);
    }

通过使用指定的java.nio.charset.Charset解码指定的字节数组,构造一个新的字符串。新String的长度是字符集的一个函数,因此可能不等于字节数组的长度。当给定字节在给定字符集中无效时,此构造函数的行为是未指定的。当需要对decodihg过程进行更多控制时,应该使用java.nio.charset.CharsetDecoder类。

     * @param  bytes        :要解码成字符的字节
     * @param  charsetName       :受支持 charset 的名称

10.

  public String(byte bytes[], Charset charset) {
	this(bytes, 0, bytes.length, charset);
    }

通过使用指定的java.nio.charset.Charset解码指定的字节数组,构造一个新的字符串。新String的长度是字符集的一个函数,因此可能不等于字节数组的长度。当给定字节在给定字符集中无效时,此构造函数的行为是未指定的。当需要对decodihg过程进行更多控制时,应该使用java.nio.charset.CharsetDecoder类。

     * @param  bytes        :要解码成字符的字节
     * @param  charset     : java.nio.charset.Charset charset用于解码字节

11.

    public String(byte bytes[], int offset, int length) {
        checkBounds(bytes, offset, length);
        this.value = StringCoding.decode(bytes, offset, length);
    }

通过使用平台的默认字符集解码指定的字节子数组,构造一个新的字符串。新的字符串的长度是字符集的一个函数,因此可能不等于子数组的长度。当给定字节在默认字符集中无效时,这个构造函数的行为是未指定的。当需要对解码过程进行更多控制时,应该使用java.nio.charset.charset.CharsetDecoder类。

     * @param  bytes        :要解码成字符的字节

    * @param  offset         :要解码的第一个字节的索引

    * @param  length        :要解码的字节数。

 

12.

    public String(byte bytes[]) {
	this(bytes, 0, bytes.length);
    }

通过使用平台的默认字符集解码指定的字节子数组,构造一个新的字符串。新的字符串的长度是字符集的一个函数,因此可能不等于子数组的长度。当给定字节在默认字符集中无效时,这个构造函数的行为是未指定的。当需要对解码过程进行更多控制时,应该使用java.nio.charset.charset.CharsetDecoder类。

   * @param  bytes        :要解码成字符的字节

 

13.

  public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }

分配一个新字符串,该字符串包含字符串缓冲区参数中当前包含的字符序列。复制字符串缓冲区的内容;字符串缓冲区的后续修改不影响新创建的字符串。

StringBuffer是线程安全的用synchronize修饰,速度较慢

* @param  buffer   :一个带缓冲区的字符串

 

14.

public String(StringBuilder builder) {
        this.value = Arrays.copyOf(builder.getValue(), builder.length());
    }

分配一个包含字符串生成器参数中当前包含的字符序列的新字符串。复制字符串生成器的内容;字符串生成器的后续修改不影响新创建的字符串。此构造函数用于简化到StringBuilder的迁移。通过 tostring方法从字符串生成器获取字符串可能运行得更快,通常是首选方法。

StringBuilder是线程不安全的,速度快

* @param  builder  :一个可变的字符序列

 

15.

  String(char[] value, boolean share) {
        // assert share : "unshared not supported";
        this.value = value;
    }

为了提高速度,将共享同一数组的私有构造函数打包

这个构造函数总是被期望用share==true来调用,因为我们已经有了一个public string(char[])构造函数,它复制了给定的char[]。

        // assert share : "unshared not supported";          “断言共享:“不支持共享”;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值