Question Sixth (formatting)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.