Java - Format Specifier Conversion Characters


Different conversion characters formats values of different data types.

For example, 's' is used to format a value as a string.

The valid values in a format specifier are determined by the conversion character and the data type.

We can classify the formatting types based on data types in four categories:

  • General formatting
  • Character formatting
  • Numeric formatting
  • Date/Time formatting

Many of the conversion characters have uppercase variants.

For example, 'S' is the uppercase variant of 's'.

The uppercase variant converts the formatted output to uppercase.

The following statement and its output demonstrate the effect of using the uppercase variant 'S'.

Note that 's' produces "Java" and 'S' produces "JAVA" for the same input value "Java".


public class Main {
  public static void main(String[] args) {
    System.out.printf("%s and %<S", "Java");
  }/*from   w w w. j  a  v  a  2*/