THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

22.1.4. Character Conversions


The character conversions apply to argument types of byte, short, and char, together with int provided
that the int value represents a valid Unicode code point (otherwise
IllegalFormatCodePointException is thrown).


The conversion indicator for characters is c or C and formats the argument as a Unicode character.


A width value specifies the minimum number of characters to output, with spaces used to pad the output as
necessary. No precision value may be supplied. The only applicable flag is to left-justify the output. If the flag
is given then a width must be given.


22.1.5. General Conversions


There are three general conversions that are unrelated but that are not numeric or character conversions, so we
describe them together. They apply to an argument of any type.


The b and B conversions are boolean conversions. If the argument is null then the output is the string
"false". If the argument is a boolean then the output is either the string "true" or the string "false"
depending on the argument's value, otherwise the output is the string "true".


The h and H conversions are hash code conversions. If the argument is null then the output is the string
"null". Otherwise, the output is the string obtained by passing the object's hashCode result to
Integer.toHexString.


The s and S conversions are string conversions. If the argument is null then the output is the string
"null". Otherwise, if the argument implements the Formattable interface (see next section) then its
formatTo method is invoked to produce a customized output; otherwise, the output is the result of invoking
toString on the argument. The # flag can be passed only to a Formattable argument, and its effect is
determined by the object.


For all the general conversions, the width specifies the minimum number of characters, while the precision
indicates the maximum. Spaces will be used to pad the output to the minimum width if necessary. The
precision is applied first so if it is smaller than the width, the output will be truncated to that maximum size.
The only flag applicable is the flag to left-justify the output. If the flag is given then a width must be given.


22.1.6. Custom Formatting


A class can support custom formatting by implementing the Formattable interface, which defines the
single method formatTo. If an instance of that class is passed as an argument for a string conversion (s or
S) then its formatTo method will be invoked, passing the current formatter as an argument.


public voidformatTo(Formatter formatter, int flags, int width,
int precision)

Outputs the representation of the current object by appending the desired text
to the destination of the given formatter, obtained from its out method (see
Section 22.1.8 on page 631). The flags, width, and precision
arguments contain the flags, width, and precision values that were used in the
string conversion format specifier. If no width or precision was given, then 1
is passed. The flags are encoded as a bit mask from the constants of the
FormattableFlags class. Only two flags are supported:
Free download pdf