25

Is this bad ?

(imagine it's bigger)

int count;
//done something to count
String myString = "this " + "is " + "my " + "string" + "and " + this.methodCall() + " answer " + "is : " + count;

or is it better in a StringBuilder/StringBuffer?

javanna
  • 59,145
  • 14
  • 144
  • 125
Mark W
  • 5,824
  • 15
  • 59
  • 97

8 Answers8

39

Java compiler will convert it into StringBuilder to increase the performance of repeated string concatenation. http://java.sun.com/docs/books/jls/third%5Fedition/html/expressions.html#15.18.1.2

Its when you are concatenating in a loop compiler can't substitute StringBuilder by itself that's when you should consider from concatenation to StringBuilder.

user765635
  • 568
  • 4
  • 11
  • What about this [article](http://www.pellegrino.link/2015/08/22/string-concatenation-with-java-8.html)? – gaheinrichs Jul 12 '16 at 21:41
  • "Its when you are concatenating in a loop compiler can't substitute StringBuilder by itself that's when you should consider from concatenation to StringBuilder" – user765635 Jul 14 '16 at 03:03
  • 1
    _Its when you are concatenating in a loop **that the** compiler can't substitute StringBuilder by itself. That's when you should consider **switching** from concatenation to **using** StringBuilder_ – Trunk Nov 12 '17 at 00:37
7

No, it's fine. If you use Sun's Java 6 compiler it will actually use StringBuilder.

Read this article

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
7

The Javadoc for StringBuffer states from Java 5.0

The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

The compiler will combine the string literals so its the same as writing

String myString = "this is my stringand " + this.methodCall() + " answer is : " + count;

which is the same as

String myString = new StringBuilder().append("this is my stringand ").append(methodCall()).append(" answer is : ").append(count).toString();

I wouldn't worry about the performance unless you need to eliminate garbage from your system, in which case you wouldn't use Strings here. (Its highly unlikely you need to worry about it)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

It would be more readable if you use StringBuilder/StringBuffer (choose based on your app Thread model)

if you write

    String str = "abc";
    str+="def";

Internally it will create StringBuilder for the same

   7:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   10:  aload_1
   11:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   14:  ldc #6; //String def
   16:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   22:  astore_1
jmj
  • 237,923
  • 42
  • 401
  • 438
3

(imagine it's bigger)...

String concatenation using the "+" operator creates many temporary objects and increases garbage collection. Using the StringBuffer class is more efficient.

More here: http://www.ibm.com/developerworks/websphere/library/bestpractices/string_concatenation.html

StringBuffer strBuf = new StringBuffer();
strBuf.append("this");
strBuf.append("is");
strBuf.append("my");
strBuf.append("string");
strBuf.append("and");
strBuf.append(this.methodCall());
strBuf.append(" answer ");
strBuf.append("is : ");
strBuf.append(count);

Can be related: String concatenation: concat() vs "+" operator

Community
  • 1
  • 1
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
2

Relatively new article with graph, Showing that concatenation with '+' scales pretty horribly. And as mentioned in another answer, StringBuilder will probably be more readable.

Thor Lancaster
  • 103
  • 1
  • 6
Per Fagrell
  • 835
  • 7
  • 15
1

You should prefer StringBuilder over string concatenation, since you have method call and variable being added to Strings.

However, the quantity of simple Strings like "this " and "is" has no effect on performance, since compiler will effectively process them and create interned Strings that will end up in the bytecode. Having this said, those mentioned Strings will have no overhead on the final performance.

ŁukaszBachman
  • 33,595
  • 11
  • 64
  • 74
1

I don't think it makes any difference to performance when a string is written like this - the compiler will optimise it anyway.

Mark Rhodes
  • 10,049
  • 4
  • 48
  • 51