26

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?

0

8 Answers 8

40

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What about this article?
"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"
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
7

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

Read this article

Comments

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)

Comments

4

(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

9 Comments

yes, but is that the case only when you are concatinating in a loop rather than building the string in one go?
Citing your source is always helpful.
@Jean-FrançoisCorbett you are right, I added the url. Thanks
When it's in a single expression like this, the compiler can optimise it for you so it doesn't create intermediate String objects. (as far as I'm aware!)
Don't use StringBuffer unless you need thread safety (which is doubtful), The Javadoc has suggested you use StringBuilder instead since 2004.
|
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

Comments

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.

Comments

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.