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 inJava 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 2, Volume I: Fundamentals (6th Edition)
by:Cay Horstmann and Gary Cornell 978-0-13-047177-2paperback
publisher:Prentice Hall B000OZ0N44kindle
published:2002-08-22
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.com American flag
American flag o’reilly safari sony e-books American 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
logofeedback Please email your feedback for publication, 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 kept confidential, not considered for posting, please explicitly specify that.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.212]
You are visitor number 57,074.