String源码分析(一)

123 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第27天,点击查看活动详情 在这里插入图片描述

🍁 作者:知识浅谈,CSDN博客专家,阿里云签约博主,InfoQ签约博主,华为云云享专家,51CTO明日之星
📌 擅长领域:全栈工程师、爬虫、ACM算法
💒 公众号:知识浅谈

String源码分析(一)总结 🤞这次都给他拿下🤞

正菜来了⛳⛳⛳

🎈String源码分析相关成员变量

🍮char[] value

含义:该值用于字符存储。 private final char value[];

🍮int hash

含义:这个函数的意思就是用来记录String对象的哈希码。 private int hash;

🍮serialVersionUID

含义:这个成员变量的含义是记录对应String类的序列化id,用于序列化和反序列化对象的时候使用。 private static final long serialVersionUID = -6849794470754667710L;

🎈String源码相关函数分析

🍮String()无参构造函数

含义:初始化一个新创建的 String 对象,使其表示一个空字符序列。请注意,由于字符串是不可变的,因此不需要使用此构造函数。

public String() {
    this.value = "".value;
}

🍮String(String original)有参构造函数

含义:初始化一个新创建的 String 对象,使其表示与参数相同的字符序列;换句话说,新创建的字符串是参数字符串的副本。除非需要原始的显式副本,否则不需要使用此构造函数,因为字符串是不可变的。

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

🍮String(char value[])

通俗解释:这个函数的主要作用就是把传入的字符数组克隆一份给当前String类对象的char数组。

public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}

🍮String(char value[], int offset, int count)

通俗解释:这个函数的意思是在valuechar数组中的从指定偏移量offset位置开始count个位置个字符初始化到String对象的底层数组中。 官方解释:分配一个新字符串,该字符串包含来自字符数组参数的子数组的字符。 offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。子数组的内容被复制;随后对字符数组的修改不会影响新创建的字符串。

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

🍮String(int[] codePoints, int offset, int count)

含义:这个和上边的很像,只不过上边的那个函数主要是针对传递进的char数组,并根据偏移量和count数量来确定截取的字符串,这个函数主要是针对整形数组进行截取,然后把int强制转化为char字符。

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

🍮String(byte ascii[], int hibyte, int offset, int count)

含义:这个函数和上边两个函数相同,都是截取数组中指定范围字符串,只不过这个是针对byte数组进行截取的。

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

🍮void checkBounds(byte[] bytes, int offset, int length)

含义:这个函数的意思是检查byte数组的边界,主要是针对offset以及length这两个位置进行判断,因为是要从offset开始截取,截取length个长度的字符。

private static void checkBounds(byte[] bytes, int offset, int length) {
    if (length < 0)
        throw new StringIndexOutOfBoundsException(length);
    if (offset < 0)
        throw new StringIndexOutOfBoundsException(offset);
    if (offset > bytes.length - length)
        throw new StringIndexOutOfBoundsException(offset + length);
}

🍚总结

以上是关于String类的相关总结,希望有所帮助。Written by 知识浅谈