printf : Java Glossary

go to home page P words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-07-15 Roedy Green, Canadian Mind Products
printf
Java 1.5+, 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 JDK 1.1+ 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)
 paperbackkindle
ISBN13:978-0-13-047177-2impressioncounterB000OZ0N44clickcounter
publisher:Prentice Hall
published:2002-08-22
by:Cay Horstmann and Gary Cornell
Covers JDK 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.
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca. Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com sony e-books American flag
Australian flag abe books anz

Learning More

Sun’s Javadoc on PrintWriter.printf : available:
Sun’s Javadoc on PrintStream.printf : available:
Sun’s Javadoc on Formatter class : available:
Sun’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
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.100]
You are visitor number 43,020.