This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 18509
    
  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)
 
 
subject: Formatting %s,%b
 
Similar Threads
Formatting doubt
Please explain the output of this: This question is from ExamLabs Diagnostic Test
examlab diagnostic test question 10
Regarding System.out.printf method
What is the output of printf?