Problem

String concatenation is nice technique used by programmers to solve some trivial problems. Java is known to be weak in some areas and some consider string concatenation to be one of them.

Solution Simple String concatenation with "+"

Perfect for simple programs without requirements for performance optimization. If you need to run this code million times it's better to use StringBuilder.

String string = "";
for (int i=0; i<10; i++) {
    string += "text";
}

Solution String concatenation with StringBuffer

This one is much faster than the simple "+" and performance efficient. For small number of iteration the performance could be similar but when it come to millions than the difference could change exponantionally.

StringBuffer string = new StringBuffer();
for (int i=0; i<10; i++) {
   string.append("text");
}

println string

Solution String concatenation with StringBuilder

The best solution in terms of perfomance. The only difference with StringBuffer is that:

StringBuilder is not synchronized and much faster than StringBuffer which is synchronized.

StringBuilder string = new StringBuilder();
for (int i=0; i<10; i++) {
    string.append("text");
}