java.lang.String format(Locale l,String format,Object…. args)

Description :

This java tutorial shows how to use the format() method of String class of java.lang package. This method returns a String representation of the result of the object args based on the format specified on the method arguments. This method is locale dependent as specified on the Locale argument.

Method Syntax :

public static String format(Locale l,String format,Object… args)

Parameter Input :

Locale|l|This specifies which locale to use in the formatting. If it’s null, the default Locale is used
String|format|Will dictate what is the format of the Object
Object|args|This object will be formatted based the format

Method Returns :

This format() method of String class change the method argument Object args into specified string format as method argument depending on the Locale specified..

Compatibility Version :

Requires Java 1.5 and up

Exception :


– The IllegalFormatException will be thrown if the argument String format is invalid.


– This exception will be thrown if and only if the format is null.

Discussion :

The string format method is a static one thus it needs to be called statically like this String.format(locale,format,args). This method is very helpful if our business requirements specified a format required.

Java Code Example :

This example source code demonstrates the use of static method format() of String class. This example simply format a float object to display only 2 decimal point thus the format is %.2f. Let’s take a look on below example source code for better understanding of the usage of format() method. We have used the Locale defined on the variable ENGLISH of Locale class.

Sample Output :

Running the format() method example source code of String class will give you the following output

Exception Scenario :

Similar Method :

  • java.lang.String format(String format,Object… args)

Suggested Reading List :

References :