StringBuilder/Buffer versus +
In short:
1. In unconditional statements the + is faster. It allready has been done during compile time.
2. In conditional statements aspecially the loops, the + has been done at runtime just as the StringBuffer and StringBuilder. In this case the StringBuilder and StringBuffer are faster.
1. In unconditional statements the + is faster. It allready has been done during compile time.
2. In conditional statements aspecially the loops, the + has been done at runtime just as the StringBuffer and StringBuilder. In this case the StringBuilder and StringBuffer are faster.
Conclusion:
Use the + only in unconditional statements.
Use a StringBuilder in conditional Statements aspecially loops.
The choice beteween the StringBuilder or the StringBuffer is simple. The StringBuffer is thread save and the Stringbuilder is not. In general that means if you do not need threadsafety, use the faster StringBuilder.
The following sites demonstrate well howto use String concat.
Use the + only in unconditional statements.
Use a StringBuilder in conditional Statements aspecially loops.
The choice beteween the StringBuilder or the StringBuffer is simple. The StringBuffer is thread save and the Stringbuilder is not. In general that means if you do not need threadsafety, use the faster StringBuilder.
The following sites demonstrate well howto use String concat.
http://kaioa.com/node/59
http://stackoverflow.com/questions/1296571/what-happens-when-java-compiler-sees-many-string-concatenations-in-one-line
http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm
http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm