Classes Involved
Home ] Up ] [ Classes Involved ] Format Specifiers ] Some Examples ]

 

 

Here are the players in this new printf game:

The class java.io.PrintStream has added new methods:
public PrintStream printf(String format,
                          Object... args)
public PrintStream printf(Locale l,
                          String format,
                          Object... args)
public PrintStream format(String format,
                          Object... args)
public PrintStream format(Locale l,
                          String format,
                          Object... args)
The class java.io.PrintWriter has added new methods:
public PrintWriter printf(String format,
                          Object... args)
public PrintWriter printf(Locale l,
                          String format,
                          Object... args)
public PrintWriter format(String format,
                          Object... args)
public PrintWriter format(Locale l,
                          String format,
                          Object... args)
The class java.lang.String has added new methods:
public static String format(String format,
                            Object... args)
public static String format(Locale l,
                            String format,
                            Object... args)
The work is actually performed by class java.util.Formatter, which is documented (for Java 5.0) here. Almost the entire javadoc for class Formatter is the specification for how such formatting is supposed to work, so it's worth a close read.

Notice that each of the above methods employs the new (as of Java 5/1.5) variable number of arguments syntax.

In each of the above methods, the parameter, format, is known as a Format String, which may contain zero or more Format Specifiers.

What's a Format Specifier?  Read on...

 
The page was last updated February 19, 2008