xrb_csdn 2018-03-29 02:23 采纳率: 100%
浏览 1021
已采纳

String类中的构造方法

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

不太明白该构造方法中if语句什么情况下会执行,原始的对象original.count什么情况下会与original.value这个数组的长度不相等呢,希望各位大侠赐教?
  • 写回答

3条回答 默认 最新

  • icyxing 2018-03-29 03:55
    关注
    String a = "123456";
    String b = a.substring(2);
    String c = new String(b);
    

    参考jdk 1.6的实现trim,subString等方法都会导致这种情况

    1.8实现方式已经变了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗