Java String Concatenation

Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more strings in Java.

  1. String concatenation using + operator

The arithmetic operator + is placed in between two string variables in order to concatenate those strings. The result can be stored in a third variable or we can directly print it using print statement.

StrConcat.java:

Output:

In the above code snippet, String s1 and s2 are concatenated using additionoperator (+) and the result is stored in String s.

  • String concatenation using concat() method

The concat() method returns a string object which stores the concatenated result of two different strings.

StrConcate.java:

Output:

In the above code snippet, the String object s stores the result of s1.concat(s2) method.

  • String concatenation using StringBuilder class

StringBuilder is class provides append() method to perform concatenation operation. The append() method accepts arguments of different types like Objects, StringBuilder, int, char, CharSequence, boolean, float, double. StringBuilder is the most popular and fastet way to concatenate strings in Java. It is mutable class which means values stored in StringBuilder objects can be updated or changed.

StrBuilder.java

Output:

                        In the above code snippet, s1, s2 and s are declared as objects of StringBuilder class.s stores the result of concatenation of s1 and s2 using append() method.

  • String concatenation using format() method

String.format() method allows to concatenate multiple strings using format specifier like %s followed by the string values or objects.

StrFormat.java

Output:

                        Here, the String objects s is assigned the concatenated result of Strings s1 and s2 using String.format() method. format() accepts parameters as format specifier followed by String objects or values.

  • String concatenation using String.join() method (Java Version 8+)

The String.join() method is available in Java version 8 and all the above versions.String.join() method accepts arguments as a separator and an array of String objects.

StrJoin.java:

Output:

In the above code snippet, the String object s stores the result of String.join(“”,s1,s2) method. A separator is specified inside quotation marks followed by the String objects or array of String objects.

  • String concatenation using StringJoiner class (Java Version 8+)

StrJoiner.java

Output:

In the above code snippet, the StringJoiner object s is declared and the constructor StringJoiner() accepts a separator value. A separator is specified inside quotation marks. The add() method appends Strings passed as arguments.

  • String concatenation using Collectors.joining() method (Java (Java Version 8+)

ColJoining.java

Output:

Here, a list of String array is declared. And a String object str stores the result of Collectors.joining() method.

  • Converting String array into String using Arrays.toString() method

Arrays.toString function is used to convert a String array into a String. This method has its definition inside Array class.

ArrtoString.java

Output:

Here, a list of String array is declared. And a String object str stores the result of Arrays.toString() method.

In this article, we have discussed different methods available to perform String concatenation in Java. For better understanding each method is explained with an example.

Pin It on Pinterest

Share This