wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Format 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 "Format" Watch "Format" New topic
Author

Format

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180


Output is 1

I thought it will expect a String parameter and gives Runtime Exception.

But for gives error.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Banu Chowdary wrote:

Output is 1

I thought it will expect a String parameter and gives Runtime Exception.


Have you looked at format() methods signature, it takes var arg type of Object., So 1 is autoboxed in Integer type. No Exception !

Banu Chowdary wrote:
But for gives error.


It doesn't give error, because it syntactically correct, but throws RuntimeException , stating "IllegalFormatConversionException: d != java.lang.String" , it means, you passed string and format specifier expect a Integer object or int.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Banu remember Object class has toString() method and Integer class overrides this method.

so it will be converted to

As format method takes var-args. And Integer.toString() will be used to print String.


will be converted to

It will not run Object class does not have any method to convert String to Integer.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Format
 
Similar Threads
Dividing a double value produces different results
Whizlabs PracticeIII Q4 Wrong?
output clarification
System format/printf
int to byte[] gives BufferOverflowException