In Java, string formatting can be done using the String.format() method
or with System.out.printf(). The format string follows the syntax
you've described, which includes various components like argument index,
flags, width, precision, and format specifiers.
%[argument index$][flags][width][.precision][format specifier]
Argument Index: Optional. Specifies the index of the argument to use.
Flags: Optional. Control various formatting options
(e.g., left justification -, leading zeroes 0, etc.).
Width: Optional. Minimum number of characters to be written.
Precision: Optional. Number of decimal places for floating-point numbers.
Format Specifier: Required. Defines the type of data and how it should be
formatted (%s for strings, %d for integers, %f for floating-point numbers, etc.).
# example
public class Example {
public static void main(String[] args) {
String name = "Lakshan";
int age = 22;
double salary = 50000.00;
System.out.printf("Name: %s, Age: %d, Salary: %.2f%n", name, age, salary);
}
}