This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conversion %s Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conversion %s" Watch "conversion %s" New topic
Author

conversion %s

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
can anyone explain this line


does it read as "we take this Long object, convert it and use it as a String.

if it has the promote mechanism,
why does

not work?
[ November 09, 2007: Message edited by: adam Lui ]

boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by adam Lui:
can anyone explain this line

does it read as "we take this Long object, convert it and use it as a String.

Kinda. The Long isn't "converted" in the sense of widening or auto-boxing. Rather, the formatter just invokes its toString() method.

if it has the promote mechanism,
why does
not work?

Because "%d" is for integers, but you're providing a double value. You need an explicit cast to make that work.

The format specifiers can be a bit confusing if you don't have a C programming background. "%d" doesn't stand for "double", but for "decimal (i.e. base 10) integer", as opposed to "%o" for octal integer and "%x" for hexadecimal integer. To format a double value, most of the time you'd use "%f" (for floating-point number, which in Java would be either a float or double).

If you're interested in the full details, take a look at the Formatter javadoc. For the SCJP exam, you just have to know the format string specifiers mentioned in K&B.


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conversion %s
 
Similar Threads
format() question
Format
formatting
output clarification
System format/printf