We can concatenate two strings in Java using the various methods as listed below:
- Using the
+=
operator - Using the
concat()
method - Using the
StringBuilder
class - Using the
format()
method - Using the
String.join()
method
Key takeaways:
String concatenation combines multiple strings into a single new string.
It’s useful in creating file paths, personalizing content, constructing URLs, bioinformatics (DNA/protein sequences), etc.
Here are some Java string concatenation methods:
Using the
+
or+=
operator: Simplest method, modifies the original stringUsing
concat()
method: Returns a new string with the appended stringUsing
StringBuilder
class: Efficient for frequent modifications, appends strings without creating new objectsUsing
format()
method: Formats strings with placeholders and valuesUsing
String.join()
method (Java 8+): Joins multiple strings with a delimiter
What do generative AI, website generators, and error alerts share? They merge strings to form meaningful text. String concatenation is a fundamental operation in many programming languages that combines two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.
It is difficult to think of a domain that doesn’t require string concatenation in one of its steps, e.g., web development, data science, and user interface development, to name just a few, wouldn’t be possible without it. Several applications rely on concatenating strings:
Creating file paths: Merging the name of the file path with its file name
Personalized content: Creating customized messages or alerts based on the user input
URL construction: Creating URLs based on the dynamic parameters and user input
Natural language processing (NLP): Constructing sentences or phrases from individual words
Bioinformatics: Joining DNA or protein sequences for comparison and analysis
James Gosling highlights the importance of versatility and simplicity in Java’s design. The following five string concatenation methods exemplify his perspective by providing an intuitive and straightforward way to concatenate strings:
+
or +=
operatorconcat()
methodStringBuilder
classformat()
methodString.join()
method+
or +=
operatorThe program below demonstrates how to concatenate two strings using the +
or +=
operator in Java. The +=
operator modifies the original string variable by adding another string to it.
class HelloWorld {public static void main( String args[] ) {String first = "Hello";String second = "World";String third = first + second;System.out.println(third);// yet another way to concatenate stringsfirst += second;System.out.println(first);}}
Note: As per the official Java documentation, when you use
+
or+=
to concatenate a string with a reference to an object in Java, the reference will automatically be converted into a string using the object'stoString()
method. If the object or itstoString()
method returnsnull
, the string "null" will be used instead.
concat()
methodThe concat()
method is a member of the String
class and is used to concatenate the specified string to the end of the current string.
The signature of the concat()
method is shown below:
String concat(String stringToConcat)
stringToConcat
is the string to be concatenated. It will be appended at the end of the string object calling this method.
The concat()
method returns the concatenated string.
The program below demonstrates the concatenation using the String.concat()
method with two strings in Java:
class HelloWorld {public static void main( String args[] ) {String first = "Hello";String second = "World";String third = first.concat(second);System.out.println(third);}}
Note: You can also directly concatenate the strings within the
println()
method itself asSystem.out.println(first + second);
orSystem.out.println(first.concat(second));
.
StringBuilder
classStringBuilder
class is an efficient way to concatenate strings in Java. It allows us to modify strings without creating intermediate string objects. We can use the StringBuilder
class to create the first string and then append it to another string using the append()
method. After creating the desired string this way, we can use the toString()
method to convert it back to a string if needed.
class HelloWorld {public static void main( String args[] ) {StringBuilder sb = new StringBuilder("Educative");sb.append(" Platform.");String concatenateStr = sb.toString();System.out.println(concatenateStr);}}
format()
methodThe format()
method in Java allows us to concatenate strings using the format specifier. The following example demonstrates how to concatenate strings using the format()
method:
class HelloWorld {public static void main( String args[] ) {String second = "Platform";String concatenateStr = String.format("Educative %s", second);System.out.println(concatenateStr);//concatenate three stringsString firstvar = "Hello";String secondvar = "Java";String thirdvar = "World";// Using String.format() to concatenate and format the stringsString concatenateStr2 = String.format("%s %s %s", firstvar, secondvar, thirdvar);// Output the resultSystem.out.println(concatenateStr2);}}
String.join()
methodThe String.join()
method (introduced in Java 8) is used to join multiple strings with a specified delimiter. The following example demonstrates how to concatenate strings using the String.join()
method:
class HelloWorld {public static void main( String args[] ) {String concatenateStr = String.join(" ", "Educative", "Platform.");System.out.println(concatenateStr);}}
Haven’t found what you were looking for? Contact Us