String concatenation is process of combining multiple strings. In Java, there are multiple ways to do this. In this tutorial, we will see several different approaches to do String concatenation in Java.
1. Using the + Operator
One of the easiest and simplest way to concatenate strings in Java is using the +
operator.
public class StringConcatenationPlusOperator { public static void main(String[] args) { // Using + operator for string concatenation String firstName = "Chaitanya"; String lastName = "Singh"; String fullName = firstName + " " + lastName; // Output: Chaitanya Singh System.out.println(fullName); } }
2. Using the concat Method
Another way of doing this is by using concat
method of String
class. I have covered this method in detail here: String concat method.
public class StringConcatenationConcatExample { public static void main(String[] args) { // Using concat method String str1 = "Beginners"; String str2 = "Book" String str3 = ".com"; String result = str1.concat(str2).concat(str3); System.out.println(result); // Output: BeginnersBook.com } }
3. Using StringBuilder or StringBuffer
StringBuilder
(or StringBuffer
for thread-safe operations) is more efficient for multiple concatenations. You just need to create an instance of StringBuilder (or StringBuffer) and then we can use append() method to concatenate multiple strings as shown in the following example.
public class StringConcatenationExample2 {
public static void main(String[] args) {
// Using StringBuilder/StringBuffer
String str1 = "Good";
String str2 = " ";
String str3 = "Morning";
String str4 = " ";
String str5 = "Guys";
StringBuilder message = new StringBuilder();
message.append(str1);
message.append(str2);
message.append(str3);
message.append(str4);
message.append(str5);
String text = message.toString();
System.out.println(text); // Output: Good Morning Guys
}
}
4. Using String.join()
We can use String.join()
method to concatenate multiple strings with a delimiter. I have covered this method in detail here: String join() method.
public class StringConcatenationJoinExample { public static void main(String[] args) { // Using String.join() String str1 = "Hello"; String str2 = "World!"; String greet = String.join(" ", str1, str2); System.out.println(greet); // Output: Hello World! } }
5. Using String.format()
There is another method String.format()
in String class which can be used for formatted string concatenation. Refer: String format() method.
public class StringConcatenationFormatExample { public static void main(String[] args) { // Using String.format() String firstName = "Chaitanya"; String lastName = "Singh"; String fullName = String.format("%s %s", firstName, lastName); System.out.println(fullName); // Output: Chaitanya Singh } }
Performance comparison between these methods
- + Operator: Simple and readable option for combining multiple strings. However, this is not an efficient way of concatenation when we need to combine multiple strings because it can create many intermediate
String
objects. - String.concat(): This is slightly better than
+
performance wise, but it is still not ideal when we require multiple concatenations. - StringBuilder/StringBuffer: This is the best method for concatenation as it doesn’t create intermediate strings.
- String.join() and String.format(): These methods are mostly used for convenience and readability purposes.
Leave a Reply