String.format
is just a standard method that consumes String
instance as a first argument.
Java doesn't compile content of string literals. It is developers responsibility to provide correct pattern.
The String.format
documentation says:
Throws:
java.util.IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
Update #1
Note: Java uses integers for char representations. So the first example won't fail.
The following code is valid in Java:
char c = 1;
int i = 'o';
Update #2
According to the documentation char
has a minimum value of 0 and a maximum value of 65535 inclusive. That is why you can't assign 65537 to a char.
On the other hand 65537 is valid argument for c
conversion in String.format
.
See the documentation:
Character - may be applied to basic types which represent Unicode characters: char, Character, byte, Byte, short, and Short. This conversion may also be applied to the types int and Integer when Character.isValidCodePoint(int) returns true