Updated March 20, 2023
Introduction to Java String Concatenation
In this article, we will explain java string concatenation in detail. Also, we will see different ways in which two or more strings can be concatenated in java. We will also see the performance of string concatenation in java. In addition, there will be java code examples showing concatenation of java strings.
Different Ways to Concatenate String in Java
Java String concatenation can be defined as the process of joining two or more strings together into a new string. The following are different ways of concatenating a string in java:
- Using + operator
- Using String.concat() method
- Using StringBuilder class
- Using StringBuffer class
1. Using + operator
This is the simplest way of concatenating java strings. Using the + operator, we can combine more than one string. One important thing to be noted is that the String obtained after concatenation using + operator will be a new String pointing to the fresh memory location in the java heap. If a string object exists in the pool, then it returns the same string object; otherwise, it creates a new object. The following are the most important things to be kept in mind while using the + operator for concatenating strings:
- Avoid the use of the + operator when concatenating strings in a loop, as it will produce a lot of garbage.
- Always store references to object returned after concatenating strings using the + operator.
- + Operator can be used for converting an integer to string in java.
2. Using a String Buffer
This is a more efficient way to concatenate java strings. Using String buffer for concatenating strings does not create a new object of the resulting string. Using String Buffer saves a lot of memory space as compared to using the + operator. When using string buffer for string concatenation, initialize string buffer object with a specified capacity equal to a number of characters in the final concatenated string. This will result in efficient memory usage as well as save time spent during the resizing of characters. The only drawback associated with using string buffer for string concatenation is the synchronization overhead involved in it as a string buffer is thread-safe.
3. Using String Builder
This is the most efficient way to concatenate java strings. Using String builder for concatenating strings does not create a new object of the resulting string. Using String Builder saves a lot of memory space as compared to using the + operator. When using string builder for string concatenation, initialize string builder object with a specified capacity equal to a number of characters in the final concatenated string. This will result in efficient memory usage as well as save time spent during the resizing of characters. Using a string builder is considered better than using a string buffer because there is no synchronization overhead involved when using a string builder.
4. Using String.concat ()
This is implemented internally using string buffer or string builder append methods which are described above.
Performance Analysis of String Concatenation Using Java
From the four-string concatenation methods described earlier, using the + operator is preferred when fewer strings are to be concatenated. But as the number of strings to be concatenated becomes larger, the performance of the + operator degrades a large number of objects are created due to string immutability. When several strings are to be concatenated, a string builder is preferred as it provides string mutability.
Syntax:
Here is the basic syntax of string concatenation in java:
1. Using + operator:
// method accepting strings to be concatenated
public String concatString(String s1, String s2){
String s3= s1+s2; // using + operator
return s3;
}
2. Using String.concat() method:
public String concatString(String s1){
String s= "First";
String s2=s.concat(s1); // using concat method
return s2;
}
3. Using a StringBuffer:
StringBuffer sb= new StringBuffer(capacity); // create string buffer instance
sb.append("first").append(" ").append("second"); // call append method specifying string to be concatenated
4. Using StringBuilder:
StringBuilder sb= new StringBuilder(capacity); // create string builder instance
sb.append("first").append(" ").append("second"); // call append method specifying string to be concatenated
Examples of String Concatenation
Given below are the examples:
Example #1
Let us see a basic example of string concatenation using the + operator and concat () method.
Code:
package com.edubca.stringconcatenation;
public class StringconcatDemo{
public static void main(String args[]){
String s1= "This";
String s2= "Is";
String s3= "Edubca";
String s4="Training";
// Using + operator
String concatenatedstr= s1 + " " + s2 +" " + s3 + " " + s4;
System.out.println("String after concatenation is :: " + concatenatedstr);
//using concat method
String concatenatedstr1= s1.concat(" ").concat(s2).concat(" ").concat(s3).concat(" ").concat(s4);
System.out.println("String after concatenation using concat method is :: " + concatenatedstr1);
}
}
Output:
Example #2
In this example, we will see how to string concatenation is achieved using a string buffer in java.
Code:
package com.edubca.stringconcatenation;
public class StringconcatDemo{
public static void main(String args[]){
String s1= "This";
String s2= "Is";
String s3= "Edubca";
String s4="Training";
// create StringBuffer Instance
StringBuffer sb=new StringBuffer("Edubca");
//call append method for string concatenate
StringBuffer concatenatedstr=sb.append(" ").append("Java").append(" ").append("Training");
System.out.println("Concatenated String using String Buffer is :: " + concatenatedstr );
}
}
Output:
Example #3
In this example, we will see how to string concatenation is achieved using a string builder in java.
Code:
package com.edubca.stringconcatenation;
public class StringconcatDemo{
public static void main(String args[]){
String s1= "This";
String s2= "Is";
String s3= "Edubca";
String s4="Training";
// create StringBuilder Instance
StringBuilder sb=new StringBuilder ("Edubca");
//call append method for string concatenate
StringBuilder concatenatedstr=sb.append(" ").append("Java").append(" ").append("Training");
System.out.println("Concatenated String using String Builder is :: " + concatenatedstr );
}
}
Output:
Conclusion
From the above discussion, we have a clear understanding of string concatenation in java, how it’s achieved, and what are different ways to concatenate java strings. Also, java code examples provide a clear understanding of concatenating strings.
Recommended Articles
This is a guide to Java String Concatenation. Here we discuss the Introduction, different Ways to Concatenate String in Java with the respective examples. You can also go through our other suggested articles to learn more –