String Concatenation in Java

Filed Under: String

String Concatenation is a very common operation in programming. In this tutorial, we will learn different ways to concatenate strings in Java.

String Concatenation in Java

We should avoid using “+” operator for string concatenation. We should use StringBuffer or StringBuilder for string concatenation.

String Concatenation using + Operator

String internally uses StringBuffer (till Java 1.4) or StringBuilder (Java 1.5 onwards) for String “+” operator calls.

Let’s write a simple String concatenation java program to prove this.

package com.journaldev.string.concatenation;

public class StringConcatenationExample {

	public static void main(String args[]){
		String str = new String ("Journal  ");
		str += "Dev!!";
	}

}

Now we will edit the same program to use StringBuffer.

package com.journaldev.string.concatenation;

public class StringConcatenationExample {

	public static void main(String args[]){
	     StringBuffer str = new StringBuffer ("Journalbytecode");
	     str.append("Dev!!");
	}

}

I have kept aside the byte code for both these programs, as you can see the only difference between both these programs are in line no 6 and 7. Let’s check out the byte code now.

String Concatenation Byte Code

˛∫æ2.com/journaldev/string/concatenation/StringConcatenationExamplejava/lang/Object<init>()VCode
	LineNumberTableLocalVariableTablethisLcom/journaldev/string/concatenation/StringConcatenationExample;main([Ljava/lang/String;)Vjava/lang/String	Journal
(Ljava/lang/String;)Vjava/lang/StringBuilder
valueOf&(Ljava/lang/Object;)Ljava/lang/String;
Dev!!
!"#append-(Ljava/lang/String;)Ljava/lang/StringBuilder;
%&'toString()Ljava/lang/String;args[Ljava/lang/String;strLjava/lang/String;
SourceFileTTT.java!/*∑±

	[ªY∑LªY+∏∑∂ ∂$L±

()
*+,-

StringBuffer append byte code

˛∫æ2#com/journaldev/string/concatenation/StringConcatenationExamplejava/lang/Object<init>()VCode
	LineNumberTableLocalVariableTablethisLcom/journaldev/string/concatenation/StringConcatenationExample;main([Ljava/lang/String;)Vjava/lang/StringBufferJournal
(Ljava/lang/String;)VDev!!
append,(Ljava/lang/String;)Ljava/lang/StringBuffer;args[Ljava/lang/String;strLjava/lang/StringBuffer;
SourceFileTTT.java!/*∑±

	NªY∑L+∂W±


 !"

From the byte code, it’s clear that String + is using the StringBuilder append method for string concatenation.

Here are the steps involved in String concatenation using + operator.

  1. A new StringBuilder object is created.
  2. String “Journal” is copied to the newly created StringBuilder object.
  3. StringBuilder append() method is called to append “Dev !!” to the StringBuilder object.
  4. StringBuilder toString() method is called to get the String object.
  5. The new String object reference is assigned to str and returned. The older string “Journal” is now available for garbage collection.

StringBuilder and StringBuffer append() Method

It’s better to use StringBuilder and StringBuffer append() method to concatenate multiple strings. These utility classes are provided to help you in common string operations.

  1. A new StringBuffer object is created with value “Journal “
  2. The append() method is called to append “Dev !!” to the object.
  3. StringBuffer toString() method is called to get the String object.

Conclusion

If you have to concatenate a few strings, it’s fine to use the + operator because there won’t be any visible performance delay. But, if you are concatenating many strings, use the StringBuilder append() method. If you are in a multi-threaded environment, then use the StringBuffer class.

You can checkout more String programs from our GitHub Repository.

Reference: String API Docs

Comments

  1. Bunthai says:

    I think there is a problem in this code.
    you forgot to close parentheses.
    please check it

    package com.journaldev.string.concatenation;

    public class StringConcatenationExample {

    public static void main(String args[]){
    StringBuffer str = new StringBuffer (“Journalbytecode
    str.append(“Dev!!”);
    }

    }

    1. Pankaj says:

      Yes, you are right. Looks like I missed them while copying from my Eclipse to here. I have corrected it now. Thanks for noticing it.

  2. Nantha kumar says:

    really amazing sir……now i can blast the answers to the interviewer

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages