java.lang.StringBuilder

System.out.println(new StringBuilder("I like")
                   .append(cake)
                   .append(" and I eat ")
                   .append(cakeNumber)
                   .append(" blah blah     prolonging this string because ")
                   .append(whyNot)
                   .append(" and so on ")
                   .append(number)
                   .append(".")
                   .toString());

java.text.MessageFormat

System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

I kind of like it with a static import, like this:

import static java.text.MessageFormat.format;

System.out.println(format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

java.util.Formatter (also known as String.format)

System.out.printf("I like %s and I eat %d blah blah    prolonging this string because %s and so on %f.%n",
                  cake, cakeNumber, whyNot, number);
                  

You have lots of syntactic choice at your disposal here (Just to list some, there is probably more):

System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))

%n represents the system's specific line break character. It is required because printf does not insert a line break after the operation automatically.


Multiline string concatenation

System.out.println("I like " + cake + " and I eat " +
                   cakeNumber + " blah blah    prolonging this string because" +
                   whyNot + " and so on " + number + ".");
Answer from randers on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are many converters, flags, and specifiers, which are documented in java.util.Formatter ... The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded. Each has a version with the following syntax: public PrintStream format(Locale l, String format, Object...
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans
Discussions

java - Formatting a long system.out.print containing many strings & integers - Stack Overflow
But the advantage of printf is that you separate the string from the variables, and also allows for better control over the display of numbers. For example, notice the %.0f. In general you can specify how much space the value should take up including padding and how many decimal places to use. ... No, it's not. Consult the documentation for java.util.Formatter ... More on stackoverflow.com
🌐 stackoverflow.com
Java - String Formatting
Go look at the documentation for String.format. It doesn't just take Strings as parameters. More on reddit.com
🌐 r/learnprogramming
2
1
November 14, 2018
[Java] How can I convert a string of long text into IPA?
Transcribing into IPA is difficult for a number of reasons. For example, which level of transcription are you after? Are you looking for the realized sounds or are you looking for the phonemes of the language? How are you planning on using this? How would you like to handle simple variations (such as the two possible vowel qualities in “the”)? How would you like to handle speaker errors? Etc. More on reddit.com
🌐 r/LanguageTechnology
6
1
November 28, 2018
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
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-format-a-Java-int-with-printf-example
How to format a Java int or long with printf example
*/ public static void main(String[] ... :: 98765 */ } } There are two key takeaways from this Java int printf example: The %d specifier works for long, int, double and short datatypes....
Top answer
1 of 6
6

java.lang.StringBuilder

System.out.println(new StringBuilder("I like")
                   .append(cake)
                   .append(" and I eat ")
                   .append(cakeNumber)
                   .append(" blah blah     prolonging this string because ")
                   .append(whyNot)
                   .append(" and so on ")
                   .append(number)
                   .append(".")
                   .toString());

java.text.MessageFormat

System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

I kind of like it with a static import, like this:

import static java.text.MessageFormat.format;

System.out.println(format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

java.util.Formatter (also known as String.format)

System.out.printf("I like %s and I eat %d blah blah    prolonging this string because %s and so on %f.%n",
                  cake, cakeNumber, whyNot, number);
                  

You have lots of syntactic choice at your disposal here (Just to list some, there is probably more):

System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))

%n represents the system's specific line break character. It is required because printf does not insert a line break after the operation automatically.


Multiline string concatenation

System.out.println("I like " + cake + " and I eat " +
                   cakeNumber + " blah blah    prolonging this string because" +
                   whyNot + " and so on " + number + ".");
2 of 6
2

Try printf

For example, you could write

System.out.printf("I like %s and I eat %d blah blah     prolonging this string because %s and so on %0.f.\n", cake, cakenumber, whyNot, number);

Also note that many IDEs (like Eclipse) will allow you to easily span strings across many lines. But the advantage of printf is that you separate the string from the variables, and also allows for better control over the display of numbers. For example, notice the %.0f. In general you can specify how much space the value should take up including padding and how many decimal places to use.

🌐
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
April 21, 2026 - All flags defined for Byte, Short, Integer, and Long apply. If the '#' flag is given, then the decimal separator will always be present. If no flags are given the default formatting is as follows: ... The width is the minimum number of characters to be written to the output. This includes any signs, digits, grouping separators, decimal separators, exponential symbol, radix indicator, parentheses, and strings ...
Find elsewhere
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - For now, just remember that we’ll use String.format() whenever we want to use this method. Inside of the method, the first input is the string that contains the placeholders. In this case, we are using three different placeholders: %s - This placeholder can be replaced by any string, or any variable which can be converted to a string. %d - This placeholder can be replaced by any integer data type, including int, short, byte, or long.
🌐
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 …
🌐
Quora
quora.com › What-is-the-best-way-to-format-long-strings-in-Java
What is the best way to format long strings in Java? - Quora
Answer (1 of 2): As you asked about “format” I guess you want to output none-string data-types inside some sentence. You have already good answers about “String.format” (or the class java.util.Formatter behind it). Check the documentation, there are a lot of format-modifiers that can ...
🌐
Coderanch
coderanch.com › t › 623115 › java › java-String-format
Using java String.format() [Solved] (Beginning Java forum at Coderanch)
This is my code for the string: This is my current output and my format string is: String format = "%-4s %-20s d"; (I used the 0's to keep track of where the itemName Ends) Based on this tutorial, -4s would mean that my itemNumber takes up 4 spaces and will be left justified, then my itemName will take up 20 spaces and would be left justified, then 04d would right justify my itemValue and pad it with 0's to the left if the value does not take up 4 character spaces.
🌐
Reddit
reddit.com › r/learnprogramming › java - string formatting
r/learnprogramming on Reddit: Java - String Formatting
November 14, 2018 -

/**

* Call String.format with three integer arguments and

* display them in reverse order.

* @param num1 50

* @param num2 60

* @param num3 70

* @param num4 80

*

* Output:

* Fourth: 50

* Third: 60

* Second: 70

* First: 80

*/

public static void reverseNums(add a parameter list here){ //#1

String reverse = String.format(add a format string here);	//#2

System.out.println(reverse);

}

We are supposed to reverse these numbers, however my question is how is it possible to pass the numbers as arguments into the String.format() method, can't I only pass in Strings? A nudge in the right direction would be a huge help. Thanks

🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java string format
The Power of Java String Format: Harnessing the Full Potential of String Manipulation
March 3, 2025 - You will learn how to setup the coding environment, I/O Model, various modules and packages, JSON & JavaScript objects. ... Talk to our experts. We are available 7 days a week, 10 AM to 7 PM ... String formatting is used in Java applications for clean, readable, and well-structured output.
🌐
Swagger
swagger.io › specification
OpenAPI Specification - Version 3.1.0 | Swagger
The keyword can be applied to either string data, including encoded binary data, or to unencoded binary data. For unencoded binary, the length is the number of octets. The following table shows how to migrate from OAS 3.0 binary data descriptions, continuing to use image/png as the example binary media type: Throughout the specification description fields are noted as supporting CommonMark markdown formatting.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › long-to-String-in-Java
long to String in Java
The easiest way to convert a long to a String is to append it to double quote. However, for complex output where a long must be embedded in a Java String, use the printf or String.format method.
🌐
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.
🌐
Dumb IT Dude
dumbitdude.com › home › string format java | formatting a string | date time format specifiers
String Format Java | Formatting a String | Date Time Format Specifiers
June 20, 2017 - Now comes the part where you learn how to use a format specifier. There are three ways to achieve that in Java: ... It is one of the most sought after ways of string format Java. The String class has a format() method in it which helps in formatting a string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-long-to-string
Java long to String | DigitalOcean
August 3, 2022 - Change the long value in above program to Octal or Hexadecimal format, you will notice that string representation is always on decimal format.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - Learn how to use Java's String.format() method to create dynamic, well-formatted strings. Explore syntax, format specifiers, and practical examples.
🌐
GeeksforGeeks
geeksforgeeks.org › java › format-specifiers-in-java
Format Specifiers in Java - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // the space format specifier import java.util.*; class GFG { public static void main(String args[]) { // create Formatter class object Formatter formatter = new Formatter(); // Use Space format specifier formatter.format("%d", -111); System.out.println(formatter); formatter = new Formatter(); formatter.format("% d", 111); System.out.println(formatter); formatter = new Formatter(); formatter.format("% d", -222); System.out.println(formatter); formatter = new Formatter(); formatter.format("% d", 222); System.out.println(formatter); } }
🌐
Udemy
blog.udemy.com › home › it & development › software development › how to format strings in java
How to Format Strings in Java - Udemy Blog
April 14, 2026 - For instance, when displaying large numbers like 100, 0000, 0000, it also displays the comma and while displaying decimal numbers, it prints numbers using proper decimal places like “134.43” as required. The need for formatted String is generally required in the modern GUI application. Java also has good formatting support for other types like Double, Integers and Date.