The strings are considered the most important part of Java programming. Strings are immutable objects that can not be changed after creation. Strings cater to multiple functionalities and coders can use these strings in methods and functions as per the project requirement. One of the most important methods of String is the format() method that is used as per the code needed.
In this article, The string format() method will be discussed in detail.
How to Employ Java String format() Method With Examples?
The format() method returns a string in a formatted way on the basis of arguments passed. The format() method has various format specifiers that are supported by Java strings to come into action when needed. This method allows to format and concatenate the string at the same time.
Let us have a look at the syntax of the format() method
format(Locale loc, String format, Object... args)
- Loc is basically the locale that is implemented by the format function.
- Format represents the string output.
- The number of arguments required by the string object “format” is termed as args.
format(String format, Object... args)
- The format represents the format in which the output of the string is displayed.
- The number of arguments required by the string object “format” is termed as args.
Example 1: Implementation of String format() Method
The below code block depicts the application of the format() method.
class FormatMethod{
public static void main(String args[])
{
//Declare the string and use format() to join the strings.
String stri1 = "Computer Systems";
//The %s outputs the String value.
String stri= String.format("The department name is %s", stri1);
System.out.println(stri);
//The %.2f prints the results for 2 decimal points.
String str2= String.format("My percentage is %.2f",89.8754);
System.out.println(str2);
}
}
In the above Java code:
- First a class named FormatMethod is declared.
- In the Main class, three strings are declared as “stri1” in which the value is declared as “Computer Systems”, “stri” and “str2” which uses the format() method to print in String in Integer.
- Using the format specifier %s the string declared as stri1 outputs the string value.
- While using the format specifier %.2f the string declared as str2 outputs the value rounded up to 2 decimal points.
- The output is printed using the println() method of Java.
Output
The output below shows the implementation of the format() method using the %s and %f format specifiers.

Example 2: Use Different Format Specifiers
In the code below we have implemented different format specifiers to check what each format specifier yields.
public class formatString {
public static void main(String[] args) {
//string 1 outputs the value of the string using the %s specifier
String stri1 = String.format("%s", "Ali");
System.out.println("My name is " +stri1);
//string 2 outputs the Decimal Integer using the %d specifier
String stri2 = String.format("%d", 88);
System.out.println("My percentage is "+stri2);
//string 3 Outputs the decimal number using the floating point specifier
String stri3 = String.format("%f", 1100.60);
System.out.println("My marks are " +stri3);
//string 4 Outputs the hex string using the hex string specifier
String stri4 = String.format("%x", 1100);
System.out.println("The hexadecimal value for marks is " +stri4);
//string 5 Outputs the Unicode character for the character specifier
String stri5 = String.format("%c", 'A');
System.out.println("The first character in my name is " + stri5);
}
}
In the above Java code:
- In the Main class, five strings are declared.
- Format specifiers like %s, %d, %f,%x, and %c are used to declare the string, integer, floating point values, the number of characters, and the Unicode character as per the need.
- The println() method prints the output.
Output
The output below shows the implementation of format specifiers in Java.

This sums up the implementation of the format() method in Java.
Conclusion
The Java string format() outputs the formatted string when declared with its specifiers. It consists of certain specifiers which are used according to the need. It not only formats the string but also prints the result in concatenated form. In this article, we have discussed in detail the format() method of Java and also implemented certain specifiers to observe how they work.