Java StringBuilder setLength 方法是否将 \0 冗余分配给其 char[] 值数组?
Does Java StringBuilder setLength method redundantly assign \0 to its char[] value array?
我正在研究 Java StringBuilder 的 setLength method。
如果新的长度更大,它将新的 "appended" 数组索引设置为 '\0':
public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
if (newLength > value.length)
expandCapacity(newLength);
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '[=11=]';
} else {
count = newLength;
}
}
这是不必要的吗?在 expandCapacity(newLength) 中,Arrays.copyOf 方法用于创建一个新的 char[] 数组,其大小为 newLength:
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
Java language specification states 数组中的组件被初始化为其默认值。对于 char,这是 '\u0000',据我所知,它是 '\0' 的 unicode 等价物。
此外,StringBuilder setLength documentation 指出:
If the newLength argument is greater than or equal to the current
length, sufficient null characters ('\u0000') are appended so that
length becomes the newLength argument.
但是这个数组的长度可以直接访问而无需为其组件赋值:
char[] array = new char[10];
System.out.println(array.length); // prints "10"
那么,setLength中的for循环是多余的吗?
当我们要重用StringBuilder
.
时是必须的
假设我们在 StringBuilder
中删除此代码
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '[=10=]';
}
我们使用以下代码进行测试:
StringBuilder builder = new StringBuilder("test");
builder.setLength(0); //the `value` still keeps "test", `count` is 0
System.out.println(builder.toString()); //print empty
builder.setLength(50); //side effect will happen here, "test" is not removed because expandCapacity still keeps the original value
System.out.println(builder.toString()); // will print test
你提到的代码在jdk6中,在java8中是不同的。
我正在研究 Java StringBuilder 的 setLength method。
如果新的长度更大,它将新的 "appended" 数组索引设置为 '\0':
public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
if (newLength > value.length)
expandCapacity(newLength);
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '[=11=]';
} else {
count = newLength;
}
}
这是不必要的吗?在 expandCapacity(newLength) 中,Arrays.copyOf 方法用于创建一个新的 char[] 数组,其大小为 newLength:
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
Java language specification states 数组中的组件被初始化为其默认值。对于 char,这是 '\u0000',据我所知,它是 '\0' 的 unicode 等价物。
此外,StringBuilder setLength documentation 指出:
If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.
但是这个数组的长度可以直接访问而无需为其组件赋值:
char[] array = new char[10];
System.out.println(array.length); // prints "10"
那么,setLength中的for循环是多余的吗?
当我们要重用StringBuilder
.
假设我们在 StringBuilder
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '[=10=]';
}
我们使用以下代码进行测试:
StringBuilder builder = new StringBuilder("test");
builder.setLength(0); //the `value` still keeps "test", `count` is 0
System.out.println(builder.toString()); //print empty
builder.setLength(50); //side effect will happen here, "test" is not removed because expandCapacity still keeps the original value
System.out.println(builder.toString()); // will print test
你提到的代码在jdk6中,在java8中是不同的。