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 Overflow
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
HTML CSS JAVASCRIPT SQL PYTHON ... INTRO TO HTML & CSS BASH RUST TOOLS ... Variables Print Variables Multiple Variables Identifiers Constants (Final) Real-Life Examples Code Challenge Java Data Types · Data Types Numbers Booleans Characters Real-Life Example Non-primitive Types The var Keyword Code Challenge Java Type Casting Java Operators · Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. Common Java types such as byte, BigDecimal, and Calendar ...
Discussions

What's the difference between String.format() and str. ...
There’s no difference in functionality. ... To me, String::formatted feels much more ergonomic. It is also easier to type with an LSP. Using the outdated String.format is more clunky and verbose. Mr. Polywhirl – Mr. Polywhirl · 2025-04-08 21:15:33 +00:00 Commented Apr 8, 2025 at 21:15 ... equivalent by specification - from the javadoc ... More on stackoverflow.com
🌐 stackoverflow.com
Java output formatting for Strings - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I was wondering if someone can show me how to use the format method for Java Strings. More on stackoverflow.com
🌐 stackoverflow.com
String.format() is 3x faster in Java 17
Glad to see some of the small enhancements we did in 17 get recognition. Not sure if this one matters, but String::format shows up in profiles every now and then so it felt reasonable to me to give it some TLC in between larger projects. More on reddit.com
🌐 r/java
41
298
October 30, 2021
s-Strings in Java?
This is called "string interpolation" and despite multiple proposals it still is not supported in Java. "String.format()" is the closest you can get. Some Java versions have a ".formatted()" method on Strings as well. More on reddit.com
🌐 r/java
23
27
March 3, 2021
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - First, instead of using an existing string variable, we are actually using the String class when we use the format() method. This is because the format() method is a static method. Static methods do not require an existing variable to use them, and can be used directly from the class where they are defined.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
June 2, 2026 - The String.format() method in Java is used to create formatted strings by combining text with values in a specified format.
🌐
Jmu
wiki.cs.jmu.edu › reference › java › formatting
Formatting Strings and Output in Java - JMU CS Wiki
January 26, 2026 - The static format() method in the String class is passed one parameter called a format string and then any number of other parameters. In the above example, the format string is "CS=" and the one other parameter is course. A format string consists of String literals and format specifiers.
Find elsewhere
🌐
CodeSignal
codesignal.com › learn › courses › java-string-manipulation-for-beginners › lessons › string-formatting-in-java-enhancing-readability-of-your-data
String Formatting in Java: Enhancing Readability of Your ...
int number = 123; double percentage = 90.32167; // `]` specifies a minimum width of 5 characters for an integer, adding extra whitespaces to the beginning // `%-5d` is the same as `]`, but whitespaces are now added to the end of the number // `%.2f` limits the output to 2 decimal digits for a float number String formatted = String.format("Number: ], Percentage: %.2f", number, percentage); System.out.println(formatted); // Prints: Number: 123, Percentage: 90.32 String formattedRight = String.format("Number: %-5d, Percentage: %.2f", number, percentage); System.out.println(formattedRight); // Prints: Number: 123 , Percentage: 90.32
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - For format specifiers that don’t correspond to arguments, the conversion is a character indicating content to be inserted in the output. ... The behavior of a null argument depends on the conversion. For example, characters s and S evaluate to null if the argument arg is null. Let’s demonstrate string formatting with an example JUnit test method:
Top answer
1 of 2
36

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)

2 of 2
20

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::formatted aids 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 String class.
  • 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 String class.
  • It's introduced in Java SE 15 (JDK 15) [since 2020].
  • This method is equivalent to String.format(this, args)
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-format-a-Java-String-with-printf-example
How to format a Java String with printf example
Use %S as the printf String specifier to output upper-case text. Precede the letter s with a number to specify field width. Put a negative sign after the % to left-justify the text. Add %n any time you wish to include a line break. Here is a simple example of how to format a Java String with printf:
🌐
Belief Driven Design
belief-driven-design.com › looking-at-java-21-string-templates-c7cbc
Looking at Java 21: String Templates | belief driven design
June 20, 2023 - As you can imagine, using format specifiers requires creating a Formatter for the template String. Even though you save on the number of String allocations, now the JVM has to parse/validate the template String. The java.text.MessageFormat type is like the older sibling of String::format, as it uses the same approach of using format String container specifiers.
🌐
Codecademy
codecademy.com › docs › java › strings › .format()
Java | Strings | .format() | Codecademy
May 29, 2025 - In Java, the .format() method returns a formatted string using the specified format string and arguments.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string format()
Java String format()
The Java string format() method is used to format strings, integers, decimal values, and so on, by using different format specifiers. This method returns the formatted string using the given locale, specified formatter, and arguments.
Published   December 24, 2024
🌐
Reddit
reddit.com › r/java › string.format() is 3x faster in java 17
r/java on Reddit: String.format() is 3x faster in Java 17
October 30, 2021 - Current PoC implementation shows numbers in line with string concatenation. But it still makes sense to improve existing APIs when we can, due the staggering amount of code that is written for them. ... The java compiler could optimize format() calls at compile time right now.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - The String.format() method in Java is a powerful tool that allows developers to create dynamic strings by inserting variable values into a predefined format. This method, borrowed from the C programming language, provides a flexible way to construct ...
🌐
Home and Learn
homeandlearn.co.uk › java › java_formatted_strings.html
Java For Complete Beginners - formatted strings
The first comma in the code above separates the format specification from the text being formatted. Here's some tables of the various options. If you want to format numbers then you can either use the "d" character or, for floating point numbers, the "f" character. Here are some code examples of String, integer and floating point formatting.
🌐
DZone
dzone.com › data engineering › data › comprehensive guide to java string formatting
Comprehensive Guide to Java String Format in 2021
July 15, 2021 - Java String formatting combines conversions, flags, widths, and precisions. We cover the basics and provide detailed tables for future reference.
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method
November 13, 2024 - Core Java Specialization | Enroll in Free Online Course Today* ... There are three primary ways to format a string in Java. You can use the String.format() method, the printf() method, or the MessageFormat class for formatting strings.
🌐
Medium
medium.com › kwal-it › how-to-format-a-string-in-java-950d07ea8be6
How To Format A String in Java. Formatted is the new kid in town | by Dieter Jordens | Kwal-IT | Medium
May 2, 2024 - Since Java 15, you can now format a String with a new method, formatted. This method is the same as the well know static String format method. From a readability perspective, this is the same as String.format. This newly added method will also be easier to test as the static alternative. Technically, you can now mock it with mockito. In practice, of course, you won’t be doing that very often.