File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting boolean, string with incompatible data types

 
Tushar Mishra
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

Can you suggest why do the following compile and run ?




Thanks
Tushar
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tushar Mishra
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic