Page 1 of 1

About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Wed Mar 23, 2011 8:23 pm
by ETS User
K&B book states that "if you have a mismatch between the type specified in your conversion character and your argument, you will get a runtime exception"

I do not understand

%b implies you are trying to print a value as a boolean. The rule for conversion to a boolean is as follows: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".

Re: About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Thu Mar 24, 2011 8:06 pm
by admin
The given explanation is correct as per the API JavaDoc at: http://download.oracle.com/javase/6/doc ... tml#syntax

Re: About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Fri Oct 14, 2011 3:44 pm
by Guest
ETS User wrote:K&B book states that "if you have a mismatch between the type specified in your conversion character and your argument, you will get a runtime exception"

I do not understand

%b implies you are trying to print a value as a boolean. The rule for conversion to a boolean is as follows: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".
I remembered the same thing in K&B, but he is right. I just tested it.

%b and %s have some slightly different rules.

I think the others follow what K&B said and I think the K&B example was using one of the ones that would cause an exception, but they made it sound like it applied to all format codes.

Re: About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Fri Oct 24, 2014 4:02 am
by piotrkmiotczyk
Any logical reason why the compiler does not see that boolean is not convertible to decimal, when using Format String Syntax?

It does see it in this case:

boolean flag = false;
float f = (float) flag; //obviously does not compile
System.out.print(f);

Re: About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Fri Oct 24, 2014 4:06 am
by admin
Because the compiler does not know that you are trying to convert a boolean to float.You are basically passing a boolean to a method that accepts a boolean. What that method does at run time to that boolean is not really known to the compiler.

Re: About Question com.enthuware.ets.scjp.v6.1.825 :

Posted: Fri Oct 24, 2014 4:10 am
by piotrkmiotczyk
Thanks.