String Formation

Read this article to learn how to format your output for specific purposes.

In Java, formatted printing is customized to accommodate the language and exploit some of its features. In this article, we will study different examples of how to format strings in Java. We will learn how to use Java’s printf method, the format method, and how to format numbers and dates.


Print Methods in Java

The print and println methods are used to display text output. "System" is a class in the Java API that contains objects and methods that perform system-level operations. One of the objects in the System class is named "out" and it has methods such as print, printf, and println, for performing output on the system console. The difference between print and println is that println advances the cursor to the next line after printing the current message.

The following is an example of the use of the println method to display a number:

Double myfloatnumber = 5.0/3.0;

This will print the following output:


The first way to interact with the Java Formatter is via the method printf. Here is a
simple example of the new printf method:

String s1 ="Hello";
String s2 ="World";
system.out.printf("%s New %s !", s1, s2);

This will print the following output:

Hello New World!

In the example, we have passed on to printf a format string with a couple of format specifiers "%s". For each of these format specifiers, we also pass a list of arguments, s1 and s2. A % in a format string designates the start of a format specifier. The end of a format specifier is indicated by one of many possible characters that are called the conversions. In the %s format specifier, the lowercase "s" indicates a string conversion.


String Formation

If we just need to create a formatted string without a need to print it, we can use the
static method format on the String class.

string s1 ="Hello";
string s2 ="World";
int a = 2;
String s =
String.format("%s New %s %d!", s1, ,a ,s2);

This will set the value of string s to:

"Hello New World 2!"

Format also uses format specifiers. Some of the format specifiers include:

%s – string conversion "my name"
%d – signed integer (decimal) conversion 100
%f – floating-point number conversion 23.34
%b – a Boolean value true or false
%e – a number in standard scientific notation 1.23e+02 same as 123

String.format can also take numeric positions:

String.format("%2$s %1$s", "s1", "s2");

This will output:

World Hello

Formatter provides a number of date-related conversions as well. The following code:

String.format("%1$td %1$tb %1$ty", new Date())

This will output:

6 February 2013

The "1$" designates which argument to use. In this example, all three format specifiers
target the first argument, i.e., "new Date()".

The last part of each format specifier is how you carry out date conversions. It uses a
two-character sequence starting with a "t". For example, "td" in the first specifier
converts the day of the month.

Java provides support for the following: layout justification and alignment, common
formats for numeric, string, and date/time data, and locale-specific output.

Source: Saylor Academy
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Thursday, April 18, 2019, 3:22 PM