Java String format Method

The Java String format method is one of the String Methods, which is to return a formatted string using the user-specified format and arguments. In this article, we will show how to write a Java String format method with an example. The basic syntax of the string.format is as shown below.

Java String.format syntax

The Java Programming Language provides two different methods to format the given string. The following method accepts two parameters: the first parameter is the format we want to apply, and the second is the arguments referenced by the format specifier.

public static String format (String format, Object.. args);  // It will return String as output 

//In order to use in program
String.format (String format, Object.. args);

The following Java string.format method accepts three parameters: first parameter is the locale you want to apply, second parameter is the format you want to apply. The third argument is the args referenced by a format specifier.

public static String format (locale l, String format, Object.. args);  // It will return String as output 

//In order to use in program
String.format (locale l, String format, Object.. args);
  • Locale: This is the locale we want to apply during the formatting. If the locale is NULL, then no localization is applied.
  • Format: Please specify the format string. For example, %d, %f, %s etc.
  • Args: Arguments referenced by the format specifier in the Format. If we specify more arguments than the format specifier, the extra arguments ignored.

IllegalFormatException: If the format string contains an illegal syntax, then the format method will throw this error. The illegal syntax may include:

  • When the format specifier is incompatible with the given arguments.
  • Or when we specify insufficient arguments.

NullPointerException: If the format is NULL.

TIP: If we omit Locale argument, then String.format will take the system default localization.

Java String.format Example

In this program, we use the String.format method to format the given object using the user-specified format.

// Java String format method example
package StringFunctions;
import java.util.Date;
import java.util.Locale;

public class FormatMethod {

	public static void main(String[] args) {
		double dbl = 98765.457896;
		
		String str1 = String.format("%f", dbl);
		System.out.println(str1);
		
		String str2 = String.format("%,2.4f", dbl);
		System.out.println(str2);
		
		String str3 = String.format("%d", 9876);
		System.out.println(str3);
		
		String str4 = String.format("-%d", 2406);
		System.out.println(str4);
		
		String str5 = String.format("Today is %tD", new Date());
		System.out.println(str5);
		
		String str6 = String.format("%s", 240.9876);
		System.out.println(str6);
		
		String str = "Tutorial";
		String std = String.format(Locale.ENGLISH, "%s Gateway",str);
		System.out.println(std);
	}
}
Java String.format Method 1

Within this example program, the following string.format statement calls the public static String format (String format, Object.. args) method to format the user-specified double value to float.

String str1 = String.format("%f", dbl);

The following format String Function code will format 9876 value to a positive integer.

String str3 = String.format("%d", 9876);

The below statement will format 2406 value to Negative integer.

String str4 = String.format("-%d", 2406);

The following Java string.format statement formats 240.9876 value to String data.

String str6 = String.format("%s", 240.9876);

The following statement calls public static String format (locale l, String format, Object.. args) method to format string data using the specified local (i.e., ENGLISH).

String std = String.format(Locale.ENGLISH, "%s Gateway",str);