my dog learned polymorphism
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

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
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 ]
I agree. Here's the link:
subject: formatting conversion
It's not a secret anymore!