Something like

String name = String.format("%s_%s%s_%s",n1,n2,n3,n4);

Using the String.format function

Using the String.format style (similar to C's 'printf' syntax) allows you to see the structure of your final string more clearly even when variable names are long. Overall it makes code easier to read then using the + operator, because you're separating your formatting text from the list of values you want in that format.

Answer from Chad Okere on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - */ String formattedString = String.format("d", str); System.out.println(formattedString); } } ... In the following example, we are using different format specifiers to display values of different types. Here we have shown few examples of how an integer value can be converted into an octal or hexadecimal value using format() method. After this example, we have shared a list of available format specifiers. public class JavaExample { public static void main(String[] args) { String str1 = String.format("%d", 15); // Integer value String str2 = String.format("%s", "BeginnersBook.com"); // String S
Discussions

How to format multiple string parameters in Java - Stack Overflow
The problem I'm having is specifying the length of the strings divName, heading1, and heading2. "%-30s" formats one of them, on either end, but for the life of me, I can't get the formatting right to set a length for each one. I tried three sets of specifiers separated by commas but it just ... More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2018
How to format and print multiple strings in Java - Stack Overflow
When outputting first and last, it combines them both. How would I go about making First and Last be outputted separately. I also tried add an output for the total number of characters would I decl... More on stackoverflow.com
🌐 stackoverflow.com
September 9, 2019
java - Do I have to specify a variable for each identical argument in String.format? - Stack Overflow
String hello = "Hello"; String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello); hello hello hello hello hello hello · Does the hello variable need to be repeated multiple times in the call to the format method or is there a shorthand version that lets you specify the ... More on stackoverflow.com
🌐 stackoverflow.com
May 10, 2019
Help with String.format
Why are you chaining the Strings with concatenation? String.format works totally different from what you do with it. With String.format you don't concatenate, you write your text with placeholders and add the values separated by commas after the text: String info = String.format("The movie %15s was directed by %15s and cost %.3f to make.", this.getTitle(), this.getDirector(), this.getProductionCost()); See: String.format and Formatter as well as the Formatting Numeric Print Output Tutorial More on reddit.com
🌐 r/learnjava
10
3
October 18, 2016
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
July 30, 2024 - One way to format Java string output is with the format method of the String class, which works like a “Java sprintf” method. Here are some examples: // output a string log.debug( String.format("The rename status is (%d)", RENAME_SUCCEEDED) ); // format some text and assign it to a string String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED); // format a string with multiple variables (interpolation) log.debug( String.format("%s is %d years old, er, young", "Al", 45) );
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - The String.format() method also enables the use of multiple format specifiers in one go, which produces a single string that contains the formatted values:
🌐
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 · Booleans Real-Life Example Code Challenge Java If...Else
Find elsewhere
🌐
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.
🌐
DZone
dzone.com › data engineering › data › a clarified string formatting cheatsheet
A Clarified String Formatting Cheatsheet - DZone
September 22, 2017 - Good to keep close by, this simplified guide to String formatting in Java covers numbers, the available flags, and handling multiple arguments.
🌐
Dot Net Perls
dotnetperls.com › format-java
Java - String.format Examples - Dot Net Perls
June 1, 2023 - Many format codes can be used with String.format. Here we pad a number with zeros on the left side.
🌐
Javaprogramto
javaprogramto.com › 2019 › 03 › java-string-format.html
Java String format() Examples JavaProgramTo.com
November 13, 2021 - ... String format method is used to format the string in the specified format. This method can be used as String output format by passing multiple arguments because the second parameter is the variable-arguments(Var-Args).
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - %s: This specifier formats strings. It’s particularly useful when you want to insert text or combine multiple strings into one.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java string format
The Power of Java String Format: Harnessing the Full Potential of String Manipulation
March 17, 2026 - These methods include StringBuilder, MessageFormat, and external libraries like Apache Commons. 1. Using StringBuilder and StringBuffer for Efficient Concatenation · Using + for string concatenation inside loops creates multiple ...
🌐
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.
🌐
Team Treehouse
teamtreehouse.com › library › java-basics-2014 › multiple-strings
Multiple Strings (How To) | Java Basics (2014) | Treehouse
October 27, 2014 - Multiple Strings. In this video, we will expand on what we've learned to allow to prompt for multiple variables and use them in a single formatted ...
🌐
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 ...
🌐
TechVidvan
techvidvan.com › tutorials › java-string-format-method
Java String format() Method with Examples - TechVidvan
February 22, 2024 - This method allows us to concatenate the texts while simultaneously formatting the resultant concatenated string. Java’s format() method is similar to the sprintf() function in C and the printf() method in Java.
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - In this code, we are using the plus symbol + to concatenate strings and variables together into a single output string. In Java, the concatenate operator will automatically convert any primitive data type to a string for us, so we don’t have to worry about that.
🌐
xperti
xperti.io › home › how to format a string in java with examples
What Is Java String Format And How To Use It?
May 5, 2022 - The formatter class (discussed after this) has many different methods to output the formatted text to a stream, two of which are format() and the printf() that we have just discussed. According to the Java documentation for Java string format, they both behave in the same way.