efo*_*ng5 5 java string constructor
我试图理解Integer .toString()的实现,如下所示:
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(0, size, buf);
}
Run Code Online (Sandbox Code Playgroud)
然后我遇到了最后一行,它看起来不像String类中的任何构造函数,除了这一行:
String(char value[], int offset, int count)
Run Code Online (Sandbox Code Playgroud)
...除了首先使用char []参数调用此函数,不像它在Integer.toString()中使用它的方式.我的印象是,改变参数的顺序计为方法签名的变化,并且会对方法进行不同的覆盖.
为什么这样做,或者我是否正确地解释了这个?
use*_*ica 10
那是使用package-private String构造函数.它没有出现在StringJavadoc中,因为它是包私有的.
如果您检查同一站点上的String源代码,您将看到
644 // Package private constructor which shares value array for speed.
645 String(int offset, int count, char value[]) {
646 this.value = value;
647 this.offset = offset;
648 this.count = count;
649 }
Run Code Online (Sandbox Code Playgroud)