Reconsidering System.out.format().
No. 143
Q: |
This exercise aims at a better understanding of
Something seems to be odd here: The format() method is being called with a different number of arguments. Actually we may call it with an arbitrary number of one or more arguments. Answer the following two questions:
|
A: |
Consider the following snippet:
This generates the following output: v=33, d=114.32, color=ff We may prettify our code to better reflect the one to one correspondence between format strings and variables:
CautionA failure in providing appropriate numbers of arguments having appropriate types likely results in a runtime exception:
v=33, d=Exception in thread "main"
java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(... |