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?
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?
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.
No, it's fine. If you use Sun's Java 6 compiler it will actually use StringBuilder.
Read this article
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)
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
(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
Relatively new article with graph, Showing that concatenation with '+' scales pretty horribly. And as mentioned in another answer, StringBuilder will probably be more readable.
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.
I don't think it makes any difference to performance when a string is written like this - the compiler will optimise it anyway.