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 - Video Materials There are also a couple of different approaches we can take to formatting output strings in Java. Let’s take a minute to review both of those and see how they work. Concatenation We’ve already seen this approach in several programs in this course.
🌐
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 - Information relevant to students, faculty, and staff at the JMU Department of Computer Science.
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 ...
Welcome! In today's lesson, we're delving into String Formatting in Java, an essential feature for presenting data in a neat manner. We'll be exploring the intricate details of format strings, as well as methods such as printf and String.format.
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - A quick example and explanation of the format API of the standard String class in Java.
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
The Java String printf method makes adding and formatting text incredibly easy. In this quick tutorial, you'll learn by example how to format, justify, pad and case output printed with Java's ...
🌐
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 - If we might have performance issues and are in doubt about what to do, benchmark it, to verify that it offers a significant performance improvement. ... String formatted(Object... args) (Java 15+)
🌐
Codecademy
codecademy.com › docs › java › strings › .format()
Java | Strings | .format() | Codecademy
May 29, 2025 - This method is part of the String class and helps improve code readability, localization support, and consistency. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more. Beginner Friendly.Beginner Friendly17 hours17 hours ... The .format...
🌐
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. If no locale is provided then it uses the default locale ...
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 - 298 votes, 41 comments. 401K subscribers in the java community. News, Technical discussions, research papers and assorted things of interest related…
🌐
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 strings with various types of data, including ...
🌐
Home and Learn
homeandlearn.co.uk › java › java_formatted_strings.html
Java For Complete Beginners - formatted strings
Have a play around with formatting, and see how you get on. If you get error messages you may have gotten your "s" formatting confused with your "d" formatting! In the next section, we'll move on and tackle Java Methods.
🌐
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 - Formatted strings not only display the string content, but they also show the content in a specified sequence. For instance, when displaying large integers like 100000000, you may want to include commas so that it appears as 100,000,000. Similarly with decimal numbers, you might want to show a specific number of decimal places like 199.53 along with rounding. Programmers will be happy to know that Java ...
🌐
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 - How To Format A String in Java Formatted is the new kid in town You probably think Java String formatting is not new for me. But actually, a lot has happened in the past versions of Java. Therefore …