简体   繁体   English

构造函数String(int,int,char [])在哪里定义?

[英]Where is the constructor String(int, int, char[]) defined?

I am trying to understand the implementation of Integer .toString(), which looks like this: 我试图理解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);
}

And I ran into the last line, which doesn't look like any of the constructors in the String class, except this one: 然后我遇到了最后一行,它看起来不像String类中的任何构造函数,除了这一行:

String(char value[], int offset, int count) 

...except that this function is called with the char[] argument first, unlike how it is being used in Integer.toString(). ...除了首先使用char []参数调用此函数,不像它在Integer.toString()中使用它的方式。 I was under the impression that changing the order of arguments counted as a change in the signature of the method, and would be a different overwrite of the method. 我的印象是,改变参数的顺序计为方法签名的变化,并且会对方法进行不同的覆盖。

Why does this work, or am I interpreting this incorrectly? 为什么这样做,或者我是否正确地解释了这个?

That's using a package-private String constructor. 那是使用包私有的String构造函数。 It doesn't show up in the String Javadoc, because it's package-private. 它没有出现在String Javadoc中,因为它是package-private。

If you check the String source code on the same site, you'll see 如果您检查同一站点上的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       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM