printf : Java Glossary


Java version 1.5 or later, printf, 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:
out.println( "x:" + x + " y:" + y );

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


book cover recommend book⇒Core Java Volume I-Fundamentals (10th Edition) (Core Series)to book home
by Cay S. Horstmann 978-0-13-417730-4 paperback
birth 1959-06-16 age:58 978-0-13-417735-9 eBook
publisher Prentice Hall B019PFBM0M kindle
published 2016-01-01
Aimed at someone who already knows C++. This is not a fluffy introductory book.
Australian flag abe books anz abe Canadian flag
German flag abe Canadian flag
German flag Chapters Indigo Canadian flag
Spanish flag Chapters Indigo eBooks Canadian flag
Spanish flag abe American flag
French flag abe American flag
French flag Barnes & Noble American flag
Italian flag abe Nook at Barnes & Noble American flag
Italian flag Kobo American flag
India flag Google play American flag
UK flag abe O’Reilly Safari American flag
UK flag Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.

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:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number