Additional Support for Formatting in the Java SE Platform APIs – Localization

Additional Support for Formatting in the Java SE Platform APIs

In this subsection we mention additional support for formatting values that is provided by various classes in the Java SE Platform APIs.

The class java.util.Formatter provides the core support for formatted text representation of primitive values and objects through its overloaded format() methods. See the Java SE Platform API documentation for the nitty-gritty details on how to specify the format.

Click here to view code image

format(String format, Object… args)
format(Locale loc, String format, Object… args)

Write a string that is a result of applying the specified format string to the values in the variable arity parameter args. The resulting string is written to the destination object that is associated with the formatter.

The destination object of a formatter is specified when the formatter is created. The destination object can, for example, be a String, a StringBuilder, a file, or any OutputStream.

Return the current formatter.

The classes java.io.PrintStream and java.io.PrintWriter also provide an overloaded format() method with the same signature for formatted output. These streams use an associated Formatter that sends the output to the PrintStream or the PrintWriter, respectively. We have used the printf() method of the PrintStream class for sending output to the terminal every time this method is invoked on the System.out field (§1.9, p. 24).

The String class also provides an analogous format() method, but it is static. This method also uses an associated Formatter for formatting purposes. Unlike the format() method of the classes mentioned earlier, this static method returns the resulting string after formatting the values (§8.4, p. 457).

The java.io.Console only provides the first form of the format() and the printf() methods (without the locale specification). These methods too use an associated Formatter. They write the resulting string to the console’s output stream, and return the current console (§20.4, p. 1256).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *