String Concatenation is a very common operation in programming. In this tutorial, we will learn different ways to concatenate strings in Java.
Table of Contents
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.
- A new StringBuilder object is created.
- String “Journal” is copied to the newly created StringBuilder object.
- StringBuilder append() method is called to append “Dev !!” to the StringBuilder object.
- StringBuilder toString() method is called to get the String object.
- 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.
- A new StringBuffer object is created with value “Journal “
- The append() method is called to append “Dev !!” to the object.
- 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.
Reference: String API Docs
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!!”);
}
}
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.
really amazing sir……now i can blast the answers to the interviewer