String concatenation
There are several ways to perform string concatenation in Java.
1. Using “+” operator
2. Using String concat() method
3. using StringBuilder and toString() method
4. using StringBuffer and toString() method
Let’s discuss each way of doing string concatenation
Using “+” operator
This is one of the easy ways of doing string concatenation
For example, “3”+”5” will result a new string “35”.
We can use this “+” operator to perform concatenation for multiple strings (more than 2 also)
For example, “1”+”2”+”3”+”4” will result a new string “1234”
Complete example
- class StringConcatenation1{
- public static void main(String args[]){
- String str="learning "+" concatenation";
- System.out.println(str);
- }
- }
class StringConcatenation1{ public static void main(String args[]){ String str="learning "+" concatenation"; System.out.println(str); } }
Points to remember
As we all know that, string is immutable, any operation including concatenation always creates a new string and keep the old string unaffected.
Whenever we use “+” operator to concatenate, internally java uses StringBuilder with toString method to achieve the concatenation.
So above concatenation line will be converted as
- String str= new StringBuilder("learning ").append(" concatenation").toString();
String str= new StringBuilder("learning ").append(" concatenation").toString();
Prefer to use it when we need to concatenate string constants
Don’t use it inside the loops.
Using String concat() method
This method concatenates the string passed as an argument to the end of the string which calls this method.
Signature of a method
- public String concat(String anotherString)
public String concat(String anotherString)
Example :
- class StringConcatenation2{
- public static void main(String args[]){
- String s1="learning ";
- String s2="concatenation";
- String s3=s1.concat(s2);
- System.out.println(s3);
- }
- }
class StringConcatenation2{ public static void main(String args[]){ String s1="learning "; String s2="concatenation"; String s3=s1.concat(s2); System.out.println(s3); } }
Points to remember
Prefer to use this concat() method when we concatenate non null string objects otherwise Calling this method with null will cause NullPointerException
If s1 is null and trying to call s1.concat(s2) will result in NullPointerException
So avoid using it when string values are dynamic(where it can be null and needs to proper null check)
Also “+” can take even integer
value to concatenate and convert that integer value as String but concat() method is very strict in what it accepts(it takes only string object)
Using StringBuilder and toString() method
In this case, we simply use the “append” method of StringBuilder and concatenate as many strings as possible and finally we will convert the concatenated StringBuilder object to String.
Example :
- public class StringConcatenation3 {
- public static void main(String[] args) {
- // Two String objects
- String str1 = "learning ";
- String str2 = "concatenation";
- // StringBuilder object
- StringBuilder sb = new StringBuilder();
- // Appending str1 and str2
- sb.append(str1).append(str2);
- // Print the result
- System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it
- }
- }
public class StringConcatenation3 { public static void main(String[] args) { // Two String objects String str1 = "learning "; String str2 = "concatenation"; // StringBuilder object StringBuilder sb = new StringBuilder(); // Appending str1 and str2 sb.append(str1).append(str2); // Print the result System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it } }
Points to remember
Prefer this way When concatenating large number of string objects in a single threaded application.
Don’t use it in Multithreaded application and when we need to append inside loop, avoid creating object inside a loop but use append inside a loop by creating object outside the loop.
Using StringBuffer and toString() method
In this case, we simply use the “append” method of StringBuffer and concatenate as many strings as possible and finally we will convert the concatenated StringBuffer object to String.
Example :
- public class StringConcatenation4 {
- public static void main(String[] args) {
- // Two String objects
- String str1 = "learning ";
- String str2 = "concatenation";
- // StringBuffer object
- StringBuffer sb = new StringBuffer();
- // Appending str1 and str2
- sb.append(str1).append(str2);
- // Print the result
- System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it
- }
- }
public class StringConcatenation4 { public static void main(String[] args) { // Two String objects String str1 = "learning "; String str2 = "concatenation"; // StringBuffer object StringBuffer sb = new StringBuffer(); // Appending str1 and str2 sb.append(str1).append(str2); // Print the result System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it } }
Points to remember
Prefer this way When concatenating large number of string objects in a multithreaded application.
Don’t use it in single threaded application as its performance is bit slower than StringBuilder and when we need to append inside loop, avoid creating object inside a loop but use append inside a loop by creating object outside the loop.
Note :
As we know that String is immutable , whenever we use concat() method for string concatenation, it will always create new string and takes lot of performance and also memory overhead.
So I would recommend to use StringBuilder to concatenate in single threaded application and StringBuffer to concatenate in multithreaded application and convert the result to string using toString() method available in both StringBuilder and StringBuffer.
If we are concatenating just 2 strings then it’s fine to use concat() method but as the count increases it worsens its performance.