I was wondering the execution speed changes if a programmer concatenates inside a Stringbuilder append() statement, or just uses two append statements instead of one.
I am asking this question to help me figure out why we use the StringBuilder class at all when we can just concatenate.
Concatenation Example:
public class MCVE {
public static void main(String[] args) {
String[] myArray = {"Some", "stuff", "to", "append", "using", "the",
"StringBuilder", "class's", "append()", "method"};
StringBuilder stringBuild = new StringBuilder();
for(String s: myArray) {
stringBuild.append(s + " ");
}
}
}
Double-Append() Example:
public class MCVE {
public static void main(String[] args) {
String[] myArray = {"Some", "stuff", "to", "append", "using", "the",
"StringBuilder", "class's", "append()", "method"};
StringBuilder stringBuild = new StringBuilder();
for(String s: myArray) {
stringBuild.append(s);
stringBuild.append(" ");
}
}
}