java.lang.String format(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 default locale.

Method Syntax :

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

Parameter Input :

DataType Parameter Description
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.

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

Sample Output :

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

java string format method example

java string format method example

Exception Scenario :

Similar Method :

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

Suggested Reading List :

References :