What are the pros and cons in different ways. I believe StringBuilder will be more efficient, as converting to a char array makes copies of the array every time you update the index.
As written, the code in the second example will create only two arrays: one when you call toCharArray() , and the other when you call String.valueOf() ( String stores data in a char [] array). The processing of the elements you perform should not initiate the distribution of objects. When you read or write an item, copies are not copied from the array.
If you intend to perform any String manipulations, it is recommended that you use StringBuilder . If you write very performance-sensitive code, and your conversion does not change the length of the string, then it would be advisable to manipulate the array directly. But since you are learning Java as a new language, I'm going to assume that you are not working in high-frequency trading or in any other environment where there is a critical delay. Therefore, you are probably better off using StringBuilder .
If you are doing any kind of conversion that can produce a string of different lengths than the original, you should almost certainly use StringBuilder ; if necessary, it will change its internal buffer.
In a related note, if you perform simple string concatenation (for example, s = "a" + someObject + "c" ), the compiler actually converts these operations to the StringBuilder.append() call chain, so you can use what you find more aesthetically pleasing. I personally prefer the + operator. However, if you create a string for multiple statements, you must create one StringBuilder .
For example:
public String toString() { return "{field1 =" + this.field1 + ", field2 =" + this.field2 + ... ", field50 =" + this.field50 + "}"; }
Here we have one long expression that includes many concatenations. You do not need to worry about optimization manually, because the compiler will use one StringBuilder and just call append() on it several times.
String s = ...; if (someCondition) { s += someValue; } s += additionalValue; return s;
This is where you get two StringBuilders created under covers, but if this is not a very hot code path in a latency-critical application, then you really should not worry. Given similar code, but with many other separate concatenations, it might be worth optimizing. The same thing happens if you know that strings can be very large. But don't just guess - measure it! Demonstrate that there is a performance problem before trying to fix this. (Note: this is just a general rule for “microoptimization," rarely there is a flaw in using StringBuilder explicitly. But do not assume that this will make a noticeable difference: if you are concerned about this, you should actually measure .)
String s = ""; for (final Object item : items) { s += item + "\n"; }
Here we perform a separate concatenation operation at each iteration of the loop, which means that a new StringBuilder will be allocated on each pass. In this case, it is probably worth using a single StringBuilder , as you may not know how large the collection will be. I would see this as an exception to “prove the performance problem before optimizing the rule”: if an operation can explode in complexity based on input, make mistakes on the side of caution.