Reputation: 8228
I have the following code fragment
System.out.printf("%b\n", 123);
which prints "true".
Can somebody explain this behavior? shouldn't this throw a IllegalFormatException?
Upvotes: 1
Views: 221
Reputation: 76908
From the JavaDocs:
If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".
The argument you're giving it isn't null
, boolean
, or Boolean
, so it falls under "Otherwise" and therefore is true
Upvotes: 2
Reputation: 30226
Well since the specification says:
"If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true". " (src)
The behavior is quite expected isn't it? Why they decided to implement it that way - no idea, I'd agree that it's not intuitive (but well it follows C which also prints just anything if you give it the wrong arguments ;) )
Upvotes: 3