• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversion %s

 
adam Lui
Ranch Hand
Posts: 186
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic