Avoid String Concatenation
If you are using strings in java , especially in a loop, don't use +
operator to connect strings.
String result = "";
for(String s: strArray)
{
result = result + s;
}
return result;
The above is bad from a performance perspective. Every time, a string is added, it creates a new String object. And if you are using a large array, that will increase your memory usage.
To avoid this memory usage, use StringBuilder class. StringBuilder makes concatenation easier.
StringBuilder sb = new StringBuilder();
for(String s: strArray)
{
sb.append(s);
}
return sb.toString();
Thank you for reading my blog. Follow me on twitter at betterjavacode