Does the following slow the performance of append method and how many String objects are created in the append ?
long[] numbers = new long[20];
StringBuffer result = new StringBuffer(20);
for (int i = 0; i <= max; i++) {
result.append(numbers[i] + " ");
}
System.out.println(result);
I mean if Strings are immutable, the append method should create one for numbers[i] then another one for the space " ", combine them into a a final one and garbage collect the two ? Am I wrong ? Is the plus sign an overkill here or should I use the following:
for (int i = 0; i <= max; i++) {
result.append(numbers[i]);
result.append(" ");
}