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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 18103
    
  39

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)
 
 
subject: Formatting %s,%b
 
Similar Threads
Formatting doubt
examlab diagnostic test question 10
What is the output of printf?
Regarding System.out.printf method
Please explain the output of this: This question is from ExamLabs Diagnostic Test