Java String format()

The String.format() method in Java is used to format a string by replacing placeholders with values. The syntax of the method is as follows:

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

The format parameter is a string that contains placeholders for the values you want to substitute. These placeholders are denoted by % followed by a character that represents the type of value to be inserted, such as d for integers, f for floating-point numbers, and s for strings.

The args parameter is a variable number of arguments that correspond to the placeholders in the format string. These arguments can be of any data type, and they will be converted to strings using the appropriate format specifier.

Here’s an example that demonstrates how to use the String.format() method:

int age = 30;
String name = "John";
double salary = 50000.00;

String output = String.format("My name is %s, I am %d years old, and my salary is $%.2f", name, age, salary);

System.out.println(output);

This will output the following string:

My name is John, I am 30 years old, and my salary is $50000.00

In this example, the %s placeholder is replaced with the name variable, the %d placeholder is replaced with the age variable, and the %f placeholder is replaced with the salary variable. The %.2f format specifier is used to display the salary with two decimal places.

Internal implementation:

The internal implementation of the String.format() method in Java uses a Formatter object to perform the formatting.

When you call the format() method, a new Formatter object is created using the provided format string. The Formatter object then processes the format string, replacing each placeholder with the corresponding value from the arguments list.

Internally, the Formatter class uses a StringBuilder to build the formatted string. It appends the individual components of the formatted string to the StringBuilder, including any literal text that appears between placeholders.

During the formatting process, the Formatter object uses various helper methods to convert the argument values to the appropriate string representation. For example, it uses the Integer.toString() method to convert an integer argument to a string, and the Double.toString() method to convert a floating-point argument to a string.

The Formatter object also supports a wide range of format specifiers, which can be used to control the formatting of the output. These format specifiers include flags, width, precision, and more.

Once the Formatter object has processed all the placeholders in the format string, it returns the final formatted string as a String object.

Overall, the String.format() method provides a flexible and powerful way to format strings in Java, and its internal implementation uses a Formatter object to handle the formatting logic.

Java String format() method example:

Sure, here’s an example that shows how to use the String.format() method in Java:

public class FormatExample {
    public static void main(String[] args) {
        String name = "John";
        int age = 30;
        double salary = 50000.00;

        String message = String.format("Hello, my name is %s. I am %d years old and I make $%.2f per year.", name, age, salary);

        System.out.println(message);
    }
}

In this example, we declare three variables: name, age, and salary. We then use the String.format() method to create a formatted string that contains these values.

The format string contains three placeholders: %s for the name variable (which is a string), %d for the age variable (which is an integer), and %.2f for the salary variable (which is a double). The %.2f format specifier is used to display the salary variable with two decimal places.

When we run the program, it will output the following string:

Hello, my name is John. I am 30 years old and I make $50000.00 per year.

This demonstrates how the String.format() method can be used to create formatted strings with dynamic content in Java.

Java String Format Specifiers:

The Java String.format() method supports a wide range of format specifiers that can be used to control the formatting of the output. Here are some of the most commonly used format specifiers:

  • %s : String. Formats the argument as a string. If the argument is not a string, it will be converted to a string using the toString() method.
  • %d : Decimal integer. Formats the argument as a decimal integer. If the argument is not an integer, it will be truncated to an integer.
  • %f : Floating-point. Formats the argument as a floating-point number. By default, it displays six digits after the decimal point. To specify a different number of decimal places, use the syntax %.[number of decimal places]f, for example %0.2f to display two decimal places.
  • %b : Boolean. Formats the argument as true or false.
  • %c : Character. Formats the argument as a single character.
  • %o : Octal. Formats the argument as an octal integer.
  • %x : Hexadecimal. Formats the argument as a hexadecimal integer, using lower-case letters.
  • %X : Hexadecimal. Formats the argument as a hexadecimal integer, using upper-case letters.
  • %e : Scientific notation. Formats the argument as a floating-point number in scientific notation, using lower-case letters.
  • %E : Scientific notation. Formats the argument as a floating-point number in scientific notation, using upper-case letters.

Here’s an example that demonstrates how to use some of these format specifiers:

public class FormatSpecifiersExample {
    public static void main(String[] args) {
        String name = "John";
        int age = 30;
        double salary = 50000.00;

        String message = String.format("Hello, my name is %s. I am %d years old and I make $%,.2f per year.", name, age, salary);

        System.out.println(message);
    }
}

In this example, we use the %s format specifier to format the name variable as a string, %d to format the age variable as an integer, and %,.2f to format the salary variable as a floating-point number with two decimal places and commas as thousands separators.

When we run the program, it will output the following string:

Hello, my name is John. I am 30 years old and I make $50,000.00 per year.

This demonstrates how to use format specifiers to create customized output in Java.