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

  1. class StringConcatenation1{  
  2.  public static void main(String args[]){  
  3.    String str="learning "+" concatenation";  
  4.    System.out.println(str);
  5.  }  
  6. }
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

  1. 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

  1. public String concat(String anotherString)
public String concat(String anotherString) 


Example :

  1. class StringConcatenation2{  
  2.  public static void main(String args[]){  
  3.    String s1="learning ";  
  4.    String s2="concatenation";  
  5.    String s3=s1.concat(s2);  
  6.    System.out.println(s3);
  7.   }  
  8. }  
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 :

  1. public class StringConcatenation3 {
  2.     public static void main(String[] args) {
  3.      // Two String objects
  4.      String str1 = "learning ";
  5.      String str2 = "concatenation";
  6.  
  7.  
  8.      // StringBuilder object
  9.      StringBuilder sb = new StringBuilder();
  10.  
  11.      // Appending str1 and str2
  12.      sb.append(str1).append(str2);
  13.  
  14.      // Print the result
  15.      System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it
  16. }
  17. }
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 :

  1. public class StringConcatenation4 {
  2.     public static void main(String[] args) {
  3.      // Two String objects
  4.      String str1 = "learning ";
  5.      String str2 = "concatenation";
  6.  
  7.  
  8.      // StringBuffer object
  9.      StringBuffer sb = new StringBuffer();
  10.  
  11.      // Appending str1 and str2
  12.      sb.append(str1).append(str2);
  13.  
  14.      // Print the result
  15.      System.out.println(sb);//sb is same as sb.toString() and hence we did not explicitly mentioned it
  16. }
  17. }
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.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on