Use StringBuilder to Concatenate Strings
To improve performance, instead of using string concatenation, use StringBuilder.append().
String objects are immutable – that is, they never change after creation. For example, consider the following code:
String str = "testing";
str = str + "abc";
str = str + "def";
The compiler translates this code as:
String str = "testing";
StringBuilder tmp = new StringBuilder(str);
tmp.append("abc");
str = tmp.toString();
StringBulder tmp = new StringBuilder(str);
tmp.append("def");
str = tmp.toString();
This copying is inherently expensive and overusing it can reduce performance significantly. You are far better off writing:
StringBuilder tmp = new StringBuilder("testing");
tmp.append("abc");
tmp.append("def");
String str = tmp.toString();
To improve performance, instead of using string concatenation, use StringBuilder.append().
String objects are immutable – that is, they never change after creation. For example, consider the following code:
String str = "testing";
str = str + "abc";
str = str + "def";
The compiler translates this code as:
String str = "testing";
StringBuilder tmp = new StringBuilder(str);
tmp.append("abc");
str = tmp.toString();
StringBulder tmp = new StringBuilder(str);
tmp.append("def");
str = tmp.toString();
This copying is inherently expensive and overusing it can reduce performance significantly. You are far better off writing:
StringBuilder tmp = new StringBuilder("testing");
tmp.append("abc");
tmp.append("def");
String str = tmp.toString();