What is the String.format() method in Java?
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
The Java String.format() method returns the formatted string by a given locale, format, and argument.
If the locale is not specified in the String.format() method, it uses the default locale by calling the Locale.getDefault() method.
Types of String.Format()
There are two types of String.format() methods:
1. public static String format(Locale loc, String form, Object… args) 2. public static String format(String form, Object… args)
Parameters
- loc : locale value to be applied on the format() method.
- form : format of the output string.
- args : specifies the number of arguments for the format string. It may be zero or more.
There is a format specifier used in the format() method that identifies the format of the return value.
Format specifiers
Format specifier | Data type | Output |
---|---|---|
%a | floating point (except BigDecimal) | returns Hex output of floating point number |
%b | Any type | “true” if non-null, “false” if null |
%c | character | Unicode character |
%d | integer (incl. byte, short, int, long, bigint) | Decimal Integer |
%e | floating point | decimal number in scientific notation |
%f | floating point | decimal number |
%g | floating point | decimal number, possibly in scientific notation depending on the precision and value. |
%h | any type | Hex String of value from hashCode() method. |
%n | none | Platform-specific line separator. |
%o | integer (incl. byte, short, int, long, bigint) | Octal number |
%s | any type | String value |
%t | Date/Time (incl. long, Calendar, Date and TemporalAccessor) | %t is the prefix for Date/Time conversions. More formatting flags are needed after this. See Date/Time conversion below. |
%x | integer (incl. byte, short, int, long, bigint) | Hex string. |
Code
The following code displays the output of the String.format() with a locale:
import java.util.Formatter;import java.util.Locale;class examplepublic static void main(String[] args)// create a new formatterStringBuffer buffer = new StringBuffer();Formatter formatter = new Formatter(buffer, Locale.US);// format a new stringString name = "Educative";formatter.format(Locale.US,"My company's name is %s !", name);// print the formatted string with specified localeSystem.out.println("" + formatter + " "+ formatter.locale());>>The following code displays the output of the String.format() by concatenating two strings:
class examplepublic static void main(String args[])String str = "Educative";// Concatenation of two stringsString s= String.format("My Company name is %s", str);System.out.println(s);>>Learn in-demand tech skills in half the time
Java.lang.String.format() Method
The java.lang.String.format(Locale l, String format, Object. args) method returns a formatted string using the specified locale, format string, and arguments.
Declaration
Following is the declaration for java.lang.String.format() method
public static String format(Locale l, String format, Object. args)Parameters
- l − This is the locale to apply during formatting. If l is null then no localization is applied.
- format − This is a format string.
- args − This is the argument referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
Return Value
This method returns a formatted string.
Exception
- IllegalFormatException − If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
- NullPointerException − If the format is null.
Example
The following example shows the usage of java.lang.String.format() method.
package com.tutorialspoint; import java.lang.*; public class StringDemo < public static void main(String[] args) < double piVal = Math.PI; /* returns a formatted string using the specified format string, and arguments */ System.out.format("%f\n", piVal); /* returns a formatted string using the specified locale, format string and arguments */ System.out.format(Locale.US, "%10.2f", piVal); >>Let us compile and run the above program, this will produce the following result −