Formatting is essential in programming to retrieve the required representations against the defined values. Also, this functionality does wonders in padding the resultant values with a desired value i.e., blank space, operator etc. In such situations, the Java String “format()” method comes into effect that is pretty much identical to the “printf()” method.
What is the Java String format() Method?
The String “format()” method retrieves a formatted string utilizing the provided locale, particular format string, and arguments.
Syntax
String format(for, Object... args)
String format(locale, for, Object... args)
In these syntaxes:
- locale: It represents the locale value to be implemented on the “format()” method.
- for: It refers to the output string’s format.
- args: These correspond to the arguments for the format string. It can be zero or more.
Return Value
It retrieves a formatted string.
Thrown Exceptions
- It throws a “NullPointerException” if the format is null.
- The “IllegalFormatException” is returned if the specified format is inappropriate or the arguments are not sufficient.
Format Specifiers in Java
Format Specifier | Data Type | Output |
---|---|---|
%d | int | Decimal Integer. |
%f | float | Decimal Number. |
%s | Any type | String Value. |
%g | float | Decimal Number in Scientific Notation. |
%x | int | Hex String. |
%t | Date/Time | Date/Time conversions. |
%o | int | Octal number. |
%h | Any type | Hex String of value via hashCode() method. |
%c | char | Unicode Character. |
%b | Any Type | True or False. |
%a | float | Returns a Hex output of floats. |
Example 1: Applying the String “format()” Method to Format Multiple Data Type Values
This example applies the “format()” method to format the integer, float, double, and string values:
public class Format {
public static void main(String args[]){
int x = 2;
float y = 2.345f;
double z = 6329381274092.2;
String w = "Linuxhint";
String in= String.format("Integer Value is %d", x);
String fl = String.format("Float Value is %.2f", y);
String db = String.format("Double Value is %f", z);
String st = String.format("String Value is %s", w);
System.out.println(in);
System.out.println(fl);
System.out.println(db);
System.out.println(st);
}}
In this code example, perform the below-stated steps:
- Initialize the “int”, “float”, “double”, and “String” values, respectively.
- After that, apply the “String.format()” method upon all these values individually with the corresponding format specifier as discussed.
- Note: The float value will be returned to 2 decimal places since the format specifier allocated is “2f”.
- Lastly, print all these values individually.
Output

This outcome verifies that the values of various data types are printed appropriately.
Example 2: Applying the String “format()” Method to Return the Octal, Hexadecimal and Scientific Notation Representation
In this demonstration, the discussed method will be utilized to return the octal, hexadecimal and scientific notation representation against the defined values:
public class Format {
public static void main(String[] args) {
int x = 5;
double y = 1542913.2;
System.out.println(String.format("Octal Representation: %o", x));
System.out.println(String.format("Hexadecimal Representation: %X", x));
System.out.println(String.format("Scientific Notation Representation: %g", y));
}}
According to this block of code:
- Initialize the given integer and double values.
- Now, return the “octal” and “hexadecimal” representation using the corresponding format specifiers by referring to the defined “int” value.
- Finally, retrieve the “scientific notation” representation against the initialized “double” value.
Output

From this output, it can be implied that the corresponding representations are returned successfully.
Example 3: Applying the String “format()” Method to Pad Values with Spaces and Zeros
This example applies the String “format()” method to pad the formatted values with blank spaces and zeros:
public class Format {
public static void main(String[] args) {
int x = 2;
String out;
out = String.format("%4d", x);
System.out.println(out);
out = String.format("%04d", x);
System.out.println(out);
}}
According to this snippet of code:
- Initialize the integer value to be formatted.
- In the next step, specify a “String” value to store the formatted values.
- Apply the “String.format()” method to format the defined integer accumulating 4 spaces, 1 for itself and 3 for the padded blank spaces and return it.
- Now, apply the method to format the integer by having 4 spaces, 1 for itself and 3 for padded zeros to the left of the integer and return it.
Output

This output implies that the padding is done accordingly in both the cases.
Conclusion
The Java String “format()” method retrieves a formatted string via the provided locale, specific format string, and arguments. This method can be applied to format the values of various data types, retrieve the hexadecimal, octal etc representation, as well as padding the formatted value.