printf : Java Glossary

printf
Java version 1.5 or later, java.io.PrintWriter. printf, java.io.PrintStream. printf and the java.util. Formatter class give you abilities similar to C’s printf. You can also use String. format to build a String using the formatting scheme. It was added in response to C++ programmers coming to Java who wanted their familiar formatting tools. Don’t confuse printf and print/ println.

It is particularly useful for making listings line up by assigning fixed widths to your fields or for displaying in hex.

G format gives you an ability like the G format in FØRTRAN, which will display in F format or E format as appropriate.

The java.text.DecimalFormat and java.text. NumberFormat classes in Java version 1.1 or later give you similar ability a bit more verbosely. Java 1.0.2 has no equivalent to the C printf or sprintf function for formatting numbers for display. You have to roll your own. One such class is available from San Diego State University called sdsu.FormatString. Acme also has one. Eliote Rusty Harold did one. Gary Cornell and Cay Horstmann’s book Core Java 2 includes one.

Most people don’t bother with printf. They use the + concatenation operator instead of printf.

// in idiomatic Java you would say:
System.out.println( "x:" + x + " y:" + y );

// instead of C-style
System.out.printf( "x: %d y: %d", x, y );

Books

book cover recommend book⇒Core Java Volume I — Fundamentals, ninth edition
by:Cay S. Horstmann, Gary Cornell 978-0-13-708189-9paperback
publisher:Prentice Hall B004Y521GIkindle
published:2012-09-10
Covers Java 1.4 such as regular expressions, New I/O, assertions, Preferences, Swing enhancements and logging. Aimed at someone who already knows C++. This is not a fluffy introductory book.
Australian flag abe books anz abe books.co.uk UK flag
German flag abe books.de amazon.co.uk UK flag
German flag amazon.de abe books.ca Canadian flag
Spanish flag iberlibro.com amazon.ca Canadian flag
Spanish flag amazon.es Chapters Indigo Canadian flag
French flag abe books.fr abe books.com American flag
French flag amazon.fr amazon.com American flag
Italian flag abe books.it Barnes & Noble American flag
Italian flag amazon.it Powells American flag
American flag O’Reilly Safari Sony e-books American flag
Kobo UN flag
Greyed out stores probably do not have the item in stock

Learning More

Oracle’s Javadoc on PrintWriter.printf : available:
Oracle’s Javadoc on PrintStream.printf : available:
Oracle’s Javadoc on Formatter class : available:
Oracle’s Javadoc on String.format : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/printf.html J:\mindprod\jgloss\printf.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.213]
You are visitor number 59,026.