aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Formatting %s,%b Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Formatting %s,%b" Watch "Formatting %s,%b" New topic
Author

Formatting %s,%b

Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
code:
----
System.out.printf("%1$s\n",89);
System.out.printf("%1$b",000);
-----

The above lines print

------
89
true
-------

why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

The JavaDoc that describes the conversion fields describes what is happening. Here is an excerpt of the relevant parts...


'b', 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".

's', If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Formatting %s,%b