
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 8, 2024
In this article, we’ll discuss the String formatting in Java using the java.util.Formatter class, which provides support for the layout justification and alignment.
Remember C’s printf? Formatting a String in Java feels very similar.
The format() method of the Formatter is exposed via a static method from the String class. This method accepts a template String and a list of arguments to populate the template with:
String greetings = String.format(
"Hello Folks, welcome to %s !",
"Baeldung");
The resulting String is:
"Hello Folks, welcome to Baeldung !"
A template is a String that contains some static text and one or more format specifiers, which indicate which argument is to be placed at the particular position.
In this case, there’s a single format specifier %s, which gets replaced by the corresponding argument.
The syntax of format specifiers for General, Character, and Numeric type is:
%[argument_index$][flags][width][.precision]conversion
Specifiers argument_index, flag, width, and precision are optional.
In our example above, if we want to specify the number of an argument explicitly, we can write it using 1$ and 2$ argument indices.
Both these being the first and second argument respectively:
String greetings = String.format(
"Hello %2$s, welcome to %1$s !",
"Baeldung",
"Folks");
%[argument_index$][flags][width]conversion
Again the argument_index, flags, and width are optional.
Let’s take an example to understand this:
@Test
public void whenFormatSpecifierForCalendar_thenGotExpected() {
Calendar c = new GregorianCalendar(2017, 11, 10);
String s = String.format(
"The date is: %tm %1$te,%1$tY", c);
assertEquals("The date is: 12 10,2017", s);
}
Here, for every format specifier, the 1st argument will be used, hence 1$. Here if we skip the argument_index for 2nd and 3rd format specifier, it tries to find 3 arguments, but we need to use the same argument for all 3 format specifiers.
So, it’s ok if we don’t specify argument _index for the first one, but we need to specify it for the other two.
The flag here is made up of two characters. Where the first character is always a ‘t’ or ‘T’. The second character depends on what part of Calendar is to be displayed.
In our example, the first format specifiers tm, indicates month formatted as two digits, te indicates the day of the month and tY indicated Year formatted as four digits.
%[flags][width]conversion
The optional flags and width are the same as defined in above sections.
The required conversion is a character or String indicating content to be inserted in the output. Currently, only the ‘%’ and newline ‘n’ can be printed using this:
@Test
public void whenNoArguments_thenExpected() {
String s = String.format("John scored 90%% in Fall semester");
assertEquals("John scored 90% in Fall semester", s);
}
Inside format(), if we want to print ‘%’ – we need to escape it by using ‘%%’.
Let’s now dig into every detail of the Format Specifier syntax, starting with a conversion. Note that you can find all the details in the Formatter javadocs.
As we noticed in the above examples, conversion part is required in all format specifiers, and it can be divided into several categories.
Let’s take a look at each one by taking examples.
Used for any argument type. The general conversions are:
We’ll now try to display boolean and String values, using the corresponding conversions:
@Test
public void givenString_whenGeneralConversion_thenConvertedString() {
String s = String.format("The correct answer is %s", false);
assertEquals("The correct answer is false", s);
s = String.format("The correct answer is %b", null);
assertEquals("The correct answer is false", s);
s = String.format("The correct answer is %B", true);
assertEquals("The correct answer is TRUE", s);
}
Used for the basic types which represent Unicode characters: char, Character, byte, Byte, short, and Short. This conversion can also be used for the types int and Integer when the Character.isValidCodePoint(int) returns true for them.
It can be written as ‘c’ or ’C’ based on the case we want.
Let’s try to print some characters:
@Test
public void givenString_whenCharConversion_thenConvertedString() {
String s = String.format("The correct answer is %c", 'a');
assertEquals("The correct answer is a", s);
s = String.format("The correct answer is %c", null);
assertEquals("The correct answer is null", s);
s = String.format("The correct answer is %C", 'b');
assertEquals("The correct answer is B", s);
s = String.format("The valid unicode character: %c", 0x0400);
assertTrue(Character.isValidCodePoint(0x0400));
assertEquals("The valid unicode character: Ѐ", s);
}
Let’s take one more example of an invalid code point:
@Test(expected = IllegalFormatCodePointException.class)
public void whenIllegalCodePointForConversion_thenError() {
String s = String.format("The valid unicode character: %c", 0x11FFFF);
assertFalse(Character.isValidCodePoint(0x11FFFF));
assertEquals("The valid unicode character: Ā", s);
}
These are used for Java integral types: byte, Byte, short, Short, int and Integer, long, Long, and BigInteger. There are three conversions in this category:
Let’s try to print each of these:
@Test
public void whenNumericIntegralConversion_thenConvertedString() {
String s = String.format("The number 25 in decimal = %d", 25);
assertEquals("The number 25 in decimal = 25", s);
s = String.format("The number 25 in octal = %o", 25);
assertEquals("The number 25 in octal = 31", s);
s = String.format("The number 25 in hexadecimal = %x", 25);
assertEquals("The number 25 in hexadecimal = 19", s);
}
Used for Java floating-point types: float, Float, double, Double, and BigDecimal
Let’s try to print the floating point numbers:
@Test
public void whenNumericFloatingConversion_thenConvertedString() {
String s = String.format(
"The computerized scientific format of 10000.00 "
+ "= %e", 10000.00);
assertEquals(
"The computerized scientific format of 10000.00 = 1.000000e+04", s);
String s2 = String.format("The decimal format of 10.019 = %f", 10.019);
assertEquals("The decimal format of 10.019 = 10.019000", s2);
}
Let’s have a look at a simple example:
@Test
public void whenLineSeparatorConversion_thenConvertedString() {
String s = String.format("First Line %nSecond Line");
assertEquals("First Line \n" + "Second Line", s);
}
Flags, in general, are used to format the output. Whereas in case of date and time, they are used to specify which part of the date is to be displayed, as we saw in the Section 4 example.
A number of flags are available, a list of which can be found in the documentation.
Let’s see a flag example to understand it’s usage. ‘-‘ is used to format the output as left justified:
@Test
public void whenSpecifyFlag_thenGotFormattedString() {
String s = String.format("Without left justified flag: %5d", 25);
assertEquals("Without left justified flag: 25", s);
s = String.format("With left justified flag: %-5d", 25);
assertEquals("With left justified flag: 25 ", s);
}
For general conversions, precision is just the maximum number of characters to be written to the output. Whereas, for the floating-point conversions the precision is the number of digits after the radix point.
The first statement is an example of precision with floating-point numbers, and the second one with general conversions:
@Test
public void whenSpecifyPrecision_thenGotExpected() {
String s = String.format(
"Output of 25.09878 with Precision 2: %.2f", 25.09878);
assertEquals("Output of 25.09878 with Precision 2: 25.10", s);
String s2 = String.format(
"Output of general conversion type with Precision 2: %.2b", true);
assertEquals("Output of general conversion type with Precision 2: tr", s2);
}
As mentioned previously, the argument_index is an integer that indicates the position of the argument in the argument list. 1$ indicates the first argument, 2$ the second argument, and so on.
Also, there is another way to reference arguments by position, by using the ‘<‘ (‘\u003c’) flag, which means the argument from the previous format specifier will be re-used. For example, these two statements would produce the identical output:
@Test
public void whenSpecifyArgumentIndex_thenGotExpected() {
Calendar c = Calendar.getInstance();
String s = String.format("The date is: %tm %1$te,%1$tY", c);
assertEquals("The date is: 12 10,2017", s);
s = String.format("The date is: %tm %<te,%<tY", c);
assertEquals("The date is: 12 10,2017", s);
}
Till now we saw the use of format() method of the Formatter class. We can also create a Formatter instance, and use that to invoke the format() method.
We can create an instance by passing in an Appendable, OutputStream, File or file name. Based on this, the formatted String is stored in an Appendable, OutputStream, File respectively.
Let’s see an example of using it with an Appendable. We can use it with others in the same way.
Let’s create a StringBuilder instance sb, and create a Formatter using it. Then we’ll invoke format() to format a String:
@Test
public void whenCreateFormatter_thenFormatterWithAppendable() {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("I am writting to a %s Instance.", sb.getClass());
assertEquals(
"I am writting to a class java.lang.StringBuilder Instance.",
sb.toString());
}
In this article, we saw the formatting facilities provided by the java.util.Formatter class. We saw various syntax that can be used to format the String and the conversion types that can be used for different data types.