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

I am trying to understand the implementation of Integer .toString (), which looks like this:

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 came across the last line, which is not like any of the constructors of the String class, except for this one:

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

... except that this function is called with the char [] argument, unlike the way it is used in Integer.toString (). I got the impression that changing the order of the arguments is considered a change in the signature of the method and will be another replacement for the method.

Why does this work, or am I misinterpreting it?

+5
source share
1 answer

What constructor is used. It does not appear in String Javadoc because it is package-private.

If you check the String source code on the same site, you will see

  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 } 
+10
source

Source: https://habr.com/ru/post/1270602/


All Articles