Format Specifiers in Java

Format Specifiers in Java thumbnail
Java formatting helps to present information to program users.

Using classes of the Java language, programmers can create formatted output with text strings and numbers. Through the String and Formatter classes, Java code can format text items for output. Format specifiers instruct Java to present data items in specific ways. Format specifiers start with the percentage sign, then list various optional parameters dictating information about the data to be formatted and how it should be presented.

  1. Index

    • Java code can include the argument index specifier as part of a formatting command. When Java programs call a formatting method, they pass this method the formatting information, any text to include and the data items to be formatted. The data items appear as an argument list, so the argument index allows code to specify particular items within this list. The following code excerpt demonstrates a section of a formatting command including the argument index:

      %2$

      Index arguments begin at position one, so this code indicates the second argument in a list.

    Flags

    • The flags specifier applies particular display rules to specified arguments. For example, a flag can indicate that Java should present certain items with particular alignment, padding, leading space and additional formatting such as parentheses around negative numbers. The flags specifier appears after the optional argument index and before the width specifier, which is also optional. The following sample excerpt indicates that a particular argument should be aligned left:

      %2$-

    Width

    • The width specifier allows code to dictate a minimum amount of characters to output. This specifier must be an integer and cannot be negative. The width specifier follows the optional flags specifier and precedes the precision specifier, also optional. The following sample code demonstrates specifying a particular width for a data item:

      %2$5

      This indicates that Java should display a minimum of five characters.

    Precision

    • The precision specifier allows Java programs to apply a restriction on the number of characters to be displayed as part of a formatting operation. The precision specifier must appear as an integer and cannot be negative. This specifier appears after the width specifier and before any conversion indicators, both of which are optional and so may not be present. The following code excerpt demonstrates a precision specifier:

      %2$5.3f

      This code indicates that Java should display three digits following the decimal place for a floating point number. The precision specifier works differently, depending on the conversion in use.

    Conversion

    • The conversion specifier is always required when code uses formatting functions; it indicates the specific formatting to use for a particular data item. This specifier must be suited to the data item in question and appears in the form of a character. The conversion specifier appears last in the list of optional specifiers, but as the only required specifier may appear in the absence of any others. The following excerpt demonstrates formatting a data item as a decimal integer:

      %2$d

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/BananaStock/Getty Images

Comments

Related Ads

Featured