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.

Answer from Martin Törnwall on Stack Overflow
Top answer
1 of 12
324

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) });
🌐
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.
🌐
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 are supported.
🌐
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.
🌐
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:
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - 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.
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
n1 in octal: 57 n1 in hexadecimal: 2f n1 in hexadecimal: 2F n1 as string: 47 n2 as string: 35.864 n3 in scientific notation: 4.45343e+07 · You can use more than one format specifier in the format string.
🌐
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 ... into every Java program. It provides a way to create formatted strings by specifying a format and then inserting one or more arguments into that format....
🌐
Learnsic
learnsic.com › blog › how-to-format-strings-in-java
How to Format Strings in Java
April 4, 2024 - The String.format() method in Java provides a powerful way to format strings. It uses a formatting string and a set of arguments to create a new formatted string. The formatting string contains placeholders that are replaced by the corresponding ...
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)
🌐
Home and Learn
homeandlearn.co.uk › java › java_formatted_strings.html
Java For Complete Beginners - formatted strings
Home and Learn: Java Programming Course · Strings of text can be formatted and output with the printf command. The printf command understands a series of characters known as a format specification. It then takes a string of text and formats it, based on the format specification passed over.
🌐
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.
🌐
Belief Driven Design
belief-driven-design.com › formatting-strings-with-java-42648c25697
Formatting Strings With Java | belief driven design
April 14, 2020 - Before that, java.text.MessageFormat was the way to bend Strings to your will. All format Strings start with % and consist of multiple optional parts and the actual conversion specifier.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - public class Example{ public static void main(String args[]){ String name = "Chaitanya"; int age = 37; double luckyNum = 123.456; String formattedString = String.format("Name: %s, Age: %d, Lucky No: $%.2f", name, age, luckyNum); System.out.println(formattedString); // Output: Name: Chaitanya, Age: 37, Lucky No: $123.456 } } import java.util.Date; public class Example{ public static void main(String args[]){ Date date = new Date(); String formattedDate = String.format("Current DateTime: %tc", date); System.out.println(formattedDate); // Output: Current DateTime: Sat Jun 08 07:39:52 PDT 2024 } }
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method - DZone
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.
🌐
Stack Abuse
stackabuse.com › how-to-format-a-string-in-java-with-examples
Format String in Java with printf(), format(), Formatter and MessageFormat
October 22, 2021 - Finally, all format specifiers, escape characters, etc. are also valid for the Formatter class as this is the main logic for formatting Strings in all three cases: String.format(), printf(), and Formatter. Finally, let's show one final formatting technique that doesn't use Formatter under the hood. MessageFormat was made to produce and provide concatenated messages in a language-neutral way. This means that the formatting will be the same, regardless of whether you're using Java, Python, or some other language that supports MessageFormat.
🌐
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