This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes formatting conversion 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 conversion" Watch "formatting conversion" New topic
Author

formatting conversion

sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
System.out.printf("%s",new Float(11)); - prints 11.0
System.out.printf("%d",new Float(11));
throw java.util.IllegalFormatConversionException: d != java.lang.Float.

first printf statement implies that (String) s == java.lang.Integer
how the first prinf statement runs fine.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
The Format string syntax API documentation says this about %s


Conversion: 's', 'S'
Argument Category: general
Description: 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().

Float doesn't appear to implement Formattable, but it has a toString() method.
[ November 23, 2006: Message edited by: Carol Enderlin ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: formatting conversion
 
It's not a secret anymore!