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)

Answer from DuncG on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
July 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 are supported.
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
The format() method returns a formatted string using a locale, format and additional arguments.
Discussions

How to format strings in Java - Stack Overflow
There are plenty of ways to format Strings using external libraries. They add little to no benefit if the libraries are imported solely for the purpose of String formatting. Few examples: Apache Commons: StringSubstitutor, examples in its JavaDoc. 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
String formatting, what's the best practice ?
3 is IMO the best but it's not supported on anything before Python 3.6. 2 is good but I personally prefer to put the numbers in the placeholders as it just makes it easier to track which of the parameters to format is slotting in each placeholder. More on reddit.com
🌐 r/learnpython
6
7
November 7, 2017
[meta] If you can't be bothered to format the code in your post correctly, I'm not going to answer

I definitely have to agree, it's very annoying to read poorly formatted code.

More on reddit.com
🌐 r/java
18
0
February 9, 2013
Top answer
1 of 2
37

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
22

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)
🌐
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.
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
3 weeks ago - Java also includes a special string method, the format() method, which allows us to use placeholders in our output string, and then replace those placeholders with the values stored in variables.
🌐
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:
Find elsewhere
Top answer
1 of 12
323

Take a look at String.format. Note, however, that it takes format specifiers similar to those of C's printf family of functions -- for example:

String.format("Hello %s, %d", "world", 42);

…would return "Hello world, 42". The "format string" link points to the complete official spec, but for simple cases, this much shorter documentation may be helpful for an introduction to format specifiers even though it's outdated and about Lava. The most commonly used ones are:

  • %s - insert a string
  • %d - insert a signed integer (decimal)
  • %f - insert a real number, standard notation

This is radically different from C#, which uses positional references with an optional format specifier. That means that you can't do things like:

String.format("The {0} is repeated again: {0}", "word");

... without actually repeating the parameter passed to printf/format. (see The Scrum Meister's comment below)


If you just want to print the result directly, you may find System.out.printf (PrintStream.printf) to your liking.

2 of 12
182

In addition to String.format, also take a look java.text.MessageFormat. The format less terse and a bit closer to the C# example you've provided and you can use it for parsing as well.

For example:

int someNumber = 42;
String someString = "foobar";
Object[] args = {new Long(someNumber), someString};
MessageFormat fmt = new MessageFormat("String is \"{1}\", number is {0}.");
System.out.println(fmt.format(args));

A nicer example takes advantage of the varargs and autoboxing improvements in Java 1.5 and turns the above into a one-liner:

MessageFormat.format("String is \"{1}\", number is {0}.", 42, "foobar");

MessageFormat is a little bit nicer for doing i18nized plurals with the choice modifier. To specify a message that correctly uses the singular form when a variable is 1 and plural otherwise, you can do something like this:

String formatString = "there were {0} {0,choice,0#objects|1#object|1<objects}";
MessageFormat fmt = new MessageFormat(formatString);
fmt.format(new Object[] { new Long(numberOfObjects) });
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
June 2, 2026 - Supports formatting of numbers, strings, dates, and other data types. Uses format specifiers such as %s, %d, %f, and %c. Example: Java program to demonstrate working of format() method
🌐
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 in Java 25
Java 25 makes simple programs concise with compact source files, instance main methods and the new IO class, with no preview flags required. ... Use %s and %S, String.formatted, field width and Java 25's IO class to format aligned, uppercase ...
Published: December 8, 2025
🌐
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 ...
Previous LessonNext Lesson: Escaping into Java: Mastering Special Character Sequences ... int number = 10; // `d` specifies the number will be returned with 5 characters in it, extra digits will be filled with 0 String formatted = String.format("Number [d]", number); System.out.println(formatted); // Prints: Number [00010]
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown. A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › Format.html
Format (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers. Format defines the programming interface for formatting locale-sensitive objects into Strings (the format method) and for parsing Strings back ...
🌐
YouTube
youtube.com › watch
How to format Strings in Java; Intro to Java (full course) Lesson 2 Video 5 - YouTube
This series of videos is suitable for programmers with minimal (or no) programming experience who want to learn Java. In this video, we will dig into String...
Published: April 20, 2024
🌐
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.
🌐
Codecademy
codecademy.com › docs › java › strings › .format()
Java | Strings | .format() | Codecademy
May 29, 2025 - ... 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() method returns the formatted version of the given string.
🌐
Educative
educative.io › answers › what-is-the-stringformat-method-in-java
What is the String.format() method in Java?
The Java String.format() method returns the formatted string by a given locale, format, and argument.
🌐
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.
🌐
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.
🌐
Better Programming
betterprogramming.pub › ways-to-java-string-formatting-d0aecc391cc9
3 Ways To Perform Java String Formatting | by Deddy Tandean | Better Programming
March 23, 2021 - 3 Ways To Perform Java String Formatting Generate pretty strings using printf(), format(), and Formatter class Since I learned Java as one of my first object-oriented programming languages, one would …