Error in String.format / Formatter?

Out of curiosity, I tried to create a really big line. It turned out that the class Formatterthrottles according to width specifications that exceed Integer.MAX_VALUE:

// Produces an empty string. (2147483648 = Integer.MAX_VALUE + 1)
String.format("%2147483648s", "");

So, I went to check that it really matches the specification. But all that he says

If the format specifier contains a width or precision with an invalid value or is not supported otherwise, then an IllegalFormatWidthException or IllegalFormatPrecisionException will be thrown.

So, in my opinion, the right behavior would be to quit IllegalFormatWidthException.

Is this error (?) Documented somewhere? (If not, I will talk about it.)


In addition, it should be noted that if you put in -front of the width (to align the left edge of the output):

String.format("%-2147483648s", "");

MissingFormatWidthException (, , ).

( OpenJDK 1.6.0_20.)

+3
2

, : IllegalFormatWidthException, .

javadoc : " , , -1 ", , , . , " " .

+2

/ , , . , .

. Java - length()

+1

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


All Articles