Run-time throws an exception
Exception in thread "main" java.util.illegalformatconversionexception:d! = java.lang.Double
At Java.util.formatter$formatspecifier.failconversion (formatter.java:4045)
At Java.util.formatter$formatspecifier.printinteger (formatter.java:2748)
At Java.util.formatter$formatspecifier.print (formatter.java:2702)
At Java.util.Formatter.format (formatter.java:2488)
At Java.io.PrintStream.format (printstream.java:970)
At Test.main (test.java:17)
I thought I'd automatically intercept it into int.
To be inquisitive.
The formatted output of the Java language is largely inspired by C-language printf . Although some format strings are similar to C, some customizations have been made to accommodate the Java language, and some of these features have been exploited. In addition, the format of Java is stricter than the format of C, for example, if the conversion is incompatible with flags, an exception is thrown.
format (String format, Object ... args)
-
Writes a formatted string to this output stream, using the specified format string and parameters.
-
-
Parameters:
-A format string that is
-
format
described in the syntax
of a format string
-
args
-a parameter referenced by the format specifier in the format string. If the arguments are more than the format specifier, the extra arguments are ignored . The number of parameters is variable and can be zero. The maximum number of parameters is limited by the maximum dimensions of the Java array defined by Java Virtual Machine specification. The behavior of the null parameter depends on the conversion.
-
Return:
-
This output stream
-
Thrown:
-
IllegalFormatException
-If the format string contains an illegal syntax, a format specifier that is incompatible with a given parameter , a parameter that is not sufficient for a given format string, or other illegal condition. For a specification of all possible formatting errors, see the Details section of the Formatter class specification.
-
NullPointerException
-If format is null
-
In short, in a word, the printf and format of Java are not automatically converted to the desired type unless the type is compatible
Question Sixth (formatting)