Java String concat() Method

String concatenation is the fundamental process of merging multiple smaller strings into a larger string. In Java, there are several approaches to accomplish string concatenation effectively. The most straightforward method is utilizing the + operator, which allows for simple joining of multiple strings. Additionally, the String.concat() method can be utilized to combine two strings in Java. However, for enhanced performance and efficiency, it is advisable to employ the StringBuffer class instead of using the + operator or String.concat() method. The methods available for string concatenation in Java encompass the following options:

Using + operator

This approach involves using the + operator to concatenate strings.

String str = "Java " + "String " + "Tutorial";

Above code will return "Java String Tutorial"

Example
class TestClass{ public static void main (String[] args){ String str = "Java " + "String " + "Tutorial"; System.out.println(str); String str1 = "Java "; String str2 = "String "; String str3 = "Tutorial"; String result = str1 + str2 + str3; System.out.println(result); } }
Output
Java String Tutorial Java String Tutorial

Using StringBuffer

In order to improve performance StringBuffer is the better choice.

The quickest way of concatenate two string using + operator.

String str = "Java"; str = str + "Tutorial";

The compiler translates this code as:

String longStr = "Java"; StringBuffer tmpSb = new StringBuffer(longStr); tmpSb.append("Tutorial"); longStr = tmpSb.toString();

One of the main factors contributing to performance degradation during string concatenation is the creation of numerous temporary String objects. This is primarily attributed to the immutability of the String class, where any modification results in the creation of a new String object.

It is important to note that in Java, the use of the + operator for string concatenation is automatically converted to the corresponding StringBuffer or StringBuilder method call, depending on the Java version. Prior to Java 1.5, StringBuffer was utilized, while from Java 1.5 onwards, StringBuilder became available as a more efficient alternative.

Considering the performance implications, the recommended approach for efficient string concatenation is to utilize the StringBuffer class. StringBuffer provides mutable character sequences, allowing for efficient appending of strings without creating unnecessary intermediate objects. By using the append() method of StringBuffer, the concatenation process can be performed seamlessly and with improved performance.

String str = new StringBuffer().append("first").append("second").append("third").toString();

Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

Example
class TestClass{ public static void main (String[] args){ String first = "Java "; String second = "String "; String third = "Tutorial"; StringBuffer str = new StringBuffer(); str.append(first); str.append(second); str.append(third); System.out.println(str); } }

Using Java String concat() method

The Java String Concat(String str) method serves the purpose of concatenating the provided String to the end of the original string. This method appends the specified string parameter to the existing string and returns a new string that represents the concatenation result.

When invoking the Concat() method, the specified string is appended to the end of the original string, forming a larger string that combines both the original string and the provided string parameter. The resulting concatenated string is then returned as a new string object, while the original string remains unchanged.

Example
class TestClass{ public static void main (String[] args){ String first = "Java "; String second = "Tutorial "; String str = first.concat(second); System.out.println(str); } }

Conclusion

Using the Concat() method, you can conveniently concatenate strings in Java, enabling the creation of composite strings by appending additional content to existing string values. This method facilitates the construction of more complex string representations and supports various string manipulation scenarios within Java applications.