This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Format" Watch "Format" New topic


Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

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: 2904

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.

I agree. Here's the link:
subject: Format
It's not a secret anymore!