wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Formatting boolean, string with incompatible data types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Formatting boolean, string with incompatible data types" Watch "Formatting boolean, string with incompatible data types" New topic
Author

Formatting boolean, string with incompatible data types

Tushar Mishra
Greenhorn

Joined: Jan 27, 2006
Posts: 7


Hi,

Can you suggest why do the following compile and run ?




Thanks
Tushar

Thanks,
Tushar
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
Hi,
I would request you to learn the Formatter classs API first.
Here is your code:


Now %b or %B is the formatted output for boolean ok....This will return null if the argument is null else if the argument is Boolean it will return String.valueOf() else it will return true. This is the algorithm for boolean.

%f is the formatted output for floating point literals and not floats or doubles but both(as far as I know) so it gives the correct output.

%s refers String. If argument is null then it returns null else if argument (class of the argument) implements Formattable interface then arg.formatTo() is invoked else arg.toString() is invoked. arg is the argument in question.

Online reference: Formatter Class API
Don't get confused Formatter class's format uses the same thing as System.out.printf() method. You can learn the API here.
I hope it answers your question.


regards,
Arka
Tushar Mishra
Greenhorn

Joined: Jan 27, 2006
Posts: 7

Thanks Arka,

This was really helpful. I compiled and ran as per the API doc for the class Formatter. It seems to work fine.

Thanks again !


Tushar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Formatting boolean, string with incompatible data types
 
Similar Threads
format() question
conversion %s
format method
System format/printf
Unboxing weirdness