Read here .
//медленно String[] fields = new String[] {"a","b","c","d","e","f","g"}; String s = ""; for (int i = 0; i < fields.length; i++) { s = s + fields[i]; } return s; //быстро String[] fields = new String[] {"a","b","c","d","e","f","g"}; StringBuilder s = new StringBuilder(); for (int i = 0; i < fields.length; i++) { s.append(fields[i]); } return s.toString();
Never use concatenation operations (operator +) strings in a loop, especially if you have many such operations, this can significantly reduce performance. All this happens because in the above example, “s = s + fileds [i]” performs as many as 3 operations: a StringBuilder is created based on the string s, the append concatenation method is called, and the toString method is called after concatenation (s = new StringBuilder (s) .append (fields [i]). toString ();). As many as 3 operations instead of one! In addition, each result s + fileds [i] will occupy the memory in the heap, as a separate line.
So the answer to your question is: yes, it makes sense to use StringBuilder
instead of concatenation.