1
StringBuffer sb = new StringBuffer();

sb.append("New "+"Delhi");

and other is:

sb.append("New ").append("Delhi");

both will print "New Delhi".

Which one is better and why?

Because some times to save time I use "+" instead of ".append".

chrisb2244
  • 2,940
  • 22
  • 44

4 Answers4

4

sb.append("New "+"Delhi"):

public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/StringBuffer
       3: dup
       4: invokespecial #3                  // Method java/lang/StringBuffer."<init>":()V
       7: astore_1
       8: aload_1
       9: ldc           #4                  // String New Delhi
      11: invokevirtual #5                  // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
      14: pop
      15: return
}

sb.append("New ").append("Delhi"):

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/StringBuffer
       3: dup
       4: invokespecial #3                  // Method java/lang/StringBuffer."<init>":()V
       7: astore_1
       8: aload_1
       9: ldc           #4                  // String New
      11: invokevirtual #5                  // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
      14: ldc           #6                  // String Delhi
      16: invokevirtual #5                  // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
      19: pop
      20: return
}

As the above bytecode, for static string:

  • when using "+", the javac compiler will auto concat it a String.

  • when using "append", the javac compiler will auto expand as two String variables.

so for static string, the "+" is good for using.

chengpohi
  • 14,064
  • 1
  • 24
  • 42
0

any string append operation is converted into StringBuilder internally like

"The answer is: " + value

is converted into :

new StringBuilder("The answer is: ")).append(value).toString()

If any expression getting concatenated is not constant , .append is a better approach.

so in your case doesn't matter performance wise which way you write. Only '+' will improve readability of your code.

Abhijeet Kale
  • 1,656
  • 1
  • 16
  • 34
0

Constant string concatenations will be replaced at compile-time.

You should use a Stringbuilder/Stringbuffer if you concatenate variable strings e.g. variables, especially when you do the concatenations in a loop.

chrisb2244
  • 2,940
  • 22
  • 44
Fabian Lang
  • 78
  • 1
  • 8
0

"+" sign is used to add a string at the end of another string. Now, as per your question, whenever append() is used with String Buffer to append character sequence or string at that time append function internally performing to concatenate string using "+" sign.

Abhishek Shah
  • 1,394
  • 1
  • 16
  • 25