我重新作了测试
如果
String s = "xxxx..."; // 1 数据长度大于65535就会出错
char[] c = new char[123456];
...
String s = new Stirng(c); // 2 数值再大也不出错。
我查了JDK的String类的sourceCode如下:
1使用的构造方法如下:
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.
v = new char[size];
System.arraycopy(originalValue, original.offset, v, 0, 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;
}
2. 构造方法如下:
public String(char value[]) {
int size = value.length;
char[] v = new char[size];
System.arraycopy(value, 0, v, 0, size);
this.offset = 0;
this.count = size;
this.value = v;
}
这两个构造方法都是把数据String or char[] 转化赋值给自己的char[] value
为什么出问题,我也解释不清楚,等待高手答复。