File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printf confusion 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 "printf confusion" Watch "printf confusion" New topic

printf confusion

Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
i can't seem to get the hang of printf. how did you guys memorize all the combinations?

Also we always need to have d in the end?

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

following are the characters those are required for outputting specific type of values
d for integers
b for booleans
s for strings
c for character and
f for float

and yes we have to give those character for respective values because the compiler gives error at runtime

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Michael Hilgert

Joined: Apr 23, 2010
Posts: 17
Yes, that's right.
First have a look to the easiest printf.
All what you need is a % and a letter described above.


Then you can add other signs.

Do you want to have less digits after the dot, use

and so on
$1 : use the first parameter

You're welcome.
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper

Joined: Apr 07, 2010
Posts: 2099

You can check the API documentation:
and choose the class format, Object... args)
If you have read that, go further on the format section: format string format
I agree. Here's the link:
subject: printf confusion
It's not a secret anymore!