Java String concatenation

String concatenation is one of the most popular habits in programming. You can concatenate strings using String, StringBuffer or StringBuilder. As you may know StringBuilder is the fastest one, as it is not thread safe and is almost identical to StringBuffer, which is synchronised. One of the worst ways to concatenate (many) strings is using the simple String class and the concatenation operator “+”. What is not so well known is that concatenation operator “+” (of String) is internally instructs machine to “create and use” a StringBuffer or StringBuilder object.
I will demonstrate this using a simple example.

public class StringConcatenation {
  public static void main(String[] args) {
    String str = "initial string";
    str = str + "additional string";

As you compiled this class, then you can disassemble the binary class file using javap and check your class internally. I will explain only the trivial.
$> javap -c StringConcatenation

Compiled from ""
public class gr.local.simple.string.StringConcatenation extends java.lang.Object{
public gr.local.simple.string.StringConcatenation();
   0:   aload_0
   1:   invokespecial   #8; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
   0:   ldc     #16; //String initial string
   2:   astore_1
   3:   new     #18; //class java/lang/StringBuilder
   6:   dup
   7:   aload_1
   8:   invokestatic    #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
   11:  invokespecial   #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   14:  ldc     #29; //String additional string
   16:  invokevirtual   #31; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #35; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   22:  astore_1
   23:  getstatic       #39; //Field java/lang/System.out:Ljava/io/PrintStream;
   26:  aload_1
   27:  invokevirtual   #45; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   30:  return

As you can see in line “11: invokespecial”, there is a StringBuilder object!!! This is because of the concatenation operator “+”. JVM creates and uses a StringBuilder object in order to do the actual concatenation of 2 strings.

If you use StringBuilder (or StringBuffer) class instead of String class, then the concatenation is much more faster (especially for multiple concatenations).

Hope is helps,
Adrianos Dadis.

Democracy Requires Free Software.


About Adrianos Dadis

Building Big Data & Streaming processing solutions in telcos business domain. Interested in distributed systems and enterprise integration.
This entry was posted in Java, Software Development and tagged , , . Bookmark the permalink.

4 Responses to Java String concatenation

  1. mapkyc says:

    Interesting. Thanks for the insight. Which Java version did you use? Does this mean that I do not have to use StringBuilder anymore, because JVM will use it anyway?
    That would be handy, because X+Y+Z is much nicer to read than “append(X).append(Y).append(Z)”

    • Adrianos Dadis says:

      Hi mapkyc, thanks for your comment.

      I am using Java 6 Hotspot.

      The most performant code is to use StringBuilder if your code is run by only one Thread. If more threads access this code, then use SringBuffer, which is synchronised. In simple string concatenation cases, like my code, the compiler will always translate ‘+’ operator to StringBuilder. But for most complex string concatenation cases (concatenate many String objects and variables) the compiler will not always build the most most performant code. So, my advice is to use ‘+’ operator for simple cases and StringBuilder (or SringBuffer) for more complex code or for code that runs very frequently.

  2. saif says:

    public class Url_to_string {

    public static void main(String[] args) {

    try { String search,search_string;
    Scanner user_query= new Scanner(;
    System.out.println(“Enter what you want to search : “);;
    StringExtractor se = new StringExtractor(“”);
    String content = se.extractStrings(false);
    String contentWithLinks = se.extractStrings(true);
    } catch (ParserException e) {
    above is my code can any body help me how can i pass search_string to google page??

Post your thought

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s