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:57 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 UK flag
German flag abe UK flag
German flag abe Canadian flag
Spanish flag Canadian flag
Spanish flag Chapters Indigo Canadian flag
French flag abe abe American flag
French flag American flag
Italian flag abe Barnes & Noble American flag
Italian flag Nook at Barnes & Noble American flag
India flag Kobo American flag
UN flag other stores Google play American flag
O’Reilly Safari American flag
Powells American flag

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:

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