Java

What is Java String format() Method With Examples?

In Java, the format() method is a built-in function that is utilized to format a string according to a set of formatting rules. The method returns an appropriate string depending on the input format string and arguments. The format() method can also be utilized to construct complex strings, such as log messages, error messages, and user interface elements, by embedding dynamic values into static strings.

This article will demonstrate the Java string format() method along with the practical implementation.

What is Java String format() Method With Examples?

The Java String format() method allows users to easily format and display data in a customizable way.

The syntax for the format() method is as follows:

String.format(String format, Object... args)

In the above syntax, the first argument is a string that holds placeholders for the dynamic values. The placeholders are represented by % symbols followed by a format specifier, which defines the type and format of the corresponding argument.

The remaining arguments are the dynamic values that are substituted into the placeholders. These can be any Java objects, including numbers, strings, dates, and other objects. Here are some examples of using the format() method:

Example 1

An example is considered in which the “format” string contains three placeholders. Let execute the code:

class HelloWorld {

public static void main(String[] args) {

String name = "Johnson";

int age = 30;

double salary = 50000.0;

String message = String.format("Hello, %s. You are %d years old and your salary is $%.2f.", name, age, salary);

System.out.println(message);

}

}

The description of the above code is given below:

  • The first placeholder is %s, which indicates a string value.
  • The second placeholder is %d, which indicates an integer value.
  • The third placeholder is %.2f, which indicates a floating-point value.
  • The corresponding arguments are the “name”, “age”, and “salary” variables, respectively

Output

The output shows that arguments are the “name”, “age”, and “salary” variables are in a format a string.

Example 2

Another example is conducted by following the format string containing two placeholders. Let follow the below code:

class World {

public static void main(String[] args) {

int num = 42;

String binary = String.format("%d in binary is %s", num, Integer.toBinaryString(num));

System.out.println(binary);

}

}

The description of the above code is mentioned below:

  • The first placeholder is %d, which indicates an integer value.
  • The second placeholder is %s, which indicates a string value.
  • The corresponding arguments are the num variable and the result of calling the toBinaryString() method on the num variable.

Output

The above display shows the binary of 42 numbers in the terminal.

Example 3

Let execute another example in which the format string contains one placeholder:

import java.time.*;

class World {

public static void main(String[] args) {

String date = "2023-05-04";

LocalDate parsedDate = LocalDate.parse(date);

String formattedDate = String.format("Today is %1$tA, %1$tB %1$te, %1$tY.", parsedDate);

System.out.println(formattedDate);

}

}

The explanation of the code is given below:

  • The “format” string contains one placeholder, which is %1$tA, %1$tB %1$te, %1$tY.
  • This placeholder indicates a date value, with the day of the week, month, day of the month, and year components separated by commas.
  • The corresponding argument is the parsedDate variable, which is a LocalDate object that represents the date parsed from the date string.

Output

The output shows day of the week, month, date, and year.

Conclusion

The format() method is a powerful tool for constructing formatted strings in Java. By using a combination of format “specifiers” and “arguments”, users can create dynamic strings that are tailored to the specific needs. With the format() method, users can manage the alignment, width, and precision of the output for generating well-formatted strings. This article has explained the format() method along with different examples.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.