String Concatenation in Java

Filed Under: Java

String Concatenation in java is very common. Being a Java developer, you are aware that every once in a while we write code for String concatenation.

String Concatenation in Java

String concatenation in java, java string concat, java string append
I am sure that you might have heard that we should not use String “+” operator for string concatenation in java. Also we should use StringBuffer or StringBuilder for this purpose. It’s a very common java interview questions and you should be prepared for it.

If you have dug deeper, you might know that String internally uses StringBuffer (till Java 1.4) or StringBuilder (Java 1.5 onwards) for String “+” operator calls.

Let’s write a very 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 very clear that internally String + is using StringBuilder append method for string concatenation in Java.

String Concatenation in java using + operator explained

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 object
  4. StringBuilder toString() method is called to get the String object
  5. The new String object reference is assigned to str and older string “Journal” is now available for garbage collection.

String concatenation using append explained

If we are using StringBuffer or StringBuilder object, it’s done in following steps:

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

Clearly second way is less time consuming and use less resources and produces less garbage collection.

If you are interested in learning more about these, read String vs StringBuffer vs StringBuilder.

Read this post to see how much StringBuilder is faster than StringBuffer in java.

You can checkout more String programs from our GitHub Repository.

Reference: String API Doc

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