🍁 作者:知识浅谈,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 知识浅谈
评论