Name
MessageFormat
Synopsis
This
class formats and substitutes objects into specified positions in a
message string (also known as the pattern string). It provides the
closest Java equivalent to the printf( )
function
of the C programming language. If a message is to be displayed only a
single time, the simplest way to use the
MessageFormat
class is through the static
format( )
method. This method is passed a message
or pattern string and an array of argument objects to be formatted
and substituted into the string. If the message is to be displayed
several times, it makes more sense to create a
MessageFormat
object, supplying the pattern
string, and then call the format( )
instance
method of this object, supplying the array of objects to be formatted
into the message.
The message or pattern string used by the
MessageFormat
contains digits enclosed in curly
braces to indicate where each argument should be substituted. The
sequence "{0}
"
indicates that the first object should be converted to a string (if
necessary) and inserted at that point, while the sequence
"{3}
" indicates
that the fourth object should be inserted. If the object to be
inserted is not a string, MessageFormat
checks to
see if it is a Date
or a subclass of
Number
. If so, it uses a default
DateFormat
or NumberFormat
object to convert the value to a string. If not, it simply invokes
the object’s toString( )
method
to convert it.
A digit within curly braces in a pattern string may be followed optionally by a comma, and ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.