Java - Format Specifier Conversion Characters

Introduction

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".

Demo

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

}