It works same as printf() of C.
%s for String
%d for int
%f for float
ahead
String.format("%02d", 8)
OUTPUT: 08
String.format("%02d", 10)
OUTPUT: 10
String.format("%04d", 10)
OUTPUT: 0010
so basically, it will pad number of 0's ahead of the expression, variable or primitive type given as the second argument, the 0's will be padded in such a way that all digits satisfies the first argument of format method of String API.
Answer from Hiren on Stack OverflowIt works same as printf() of C.
%s for String
%d for int
%f for float
ahead
String.format("%02d", 8)
OUTPUT: 08
String.format("%02d", 10)
OUTPUT: 10
String.format("%04d", 10)
OUTPUT: 0010
so basically, it will pad number of 0's ahead of the expression, variable or primitive type given as the second argument, the 0's will be padded in such a way that all digits satisfies the first argument of format method of String API.
It just takes the variables hour, minute, and second and bring it the the format 05:23:42. Maybe another example would be this:
String s = String.format("Hello %s answered your question", "placeofm");
When you print the string to show it in the console it would look like this
System.out.println(s);
Hello placeofm answered your question
The placeholder %02d is for a decimal number with two digits. %s is for a String like my name in the sample above. If you want to learn Java you have to read the docs. Here it is for the String class. You can find the format method with a nice explanation. To read this docs is really important not even in Java.
What's the difference between String.format() and str. ...
String.format() is 3x faster in Java 17
is there any difference between using string.format() or an fstring?
Help with String.format
Videos
Make sure you use a good IDE so that you have easy access to browse into JDK source code. In Eclipse say, use F3 to open to any declaration. IntelliJ IDEA has similar feature.
If you view the source code for both methods, you can see these calls are identical except that variables this is interchanged with format when comparing the instance vs static method:
public String formatted(Object... args) {
return new Formatter().format(this, args).toString();
}
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
So as you've observed: String.format(str, args) is same as str.formatted(args)
Text Blocks were finalized and permanently added in JDK 15 and some additional methods added to support text blocks. One of this methods is:
String::formatted(Object... args)
And I know the functions of two codes below are the same.
As you mentioned in your question both methods do the same job and return same results. The goal of introducing such a method is:
simplify value substitution in the Text Block.
Based on JEP (JDK Enhancement Proposals) 378:
Text blocks do not directly support string interpolation. Interpolation may be considered in a future JEP. In the meantime, the new instance method
String::formattedaids in situations where interpolation might be desired.
As an example you consider this code segment:
String code = String.format("""
public void print(%s o) {
System.out.println(Objects.toString(o));
}
""", type);
We can change it using formatted method as:
String source = """
public void print(%s object) {
System.out.println(Objects.toString(object));
}
""".formatted(type);
Which is cleaner.
Also consider these minor differences between them when using the methods:
public static String format(String format, Object... args)
- Returns a formatted string using the format string and arguments.
- It's a static method of
Stringclass. - It's introduced in Java SE 5 [since 2004].
public String formatted(Object... args)
- Formats using this string as the format string, and the supplied arguments.
- It's an instance method of
Stringclass. - It's introduced in Java SE 15 (JDK 15) [since 2020].
- This method is equivalent to
String.format(this, args)