The main difference (and the reason the compiler uses StringBuilder to concatenate strings) is that String is immutable, while StringBuilder not.
For example, to calculate s1 + s2 + s3 using only strings, you would need to copy two s1 characters. This can be avoided (s) by using StringBuilder .
This optimization is explicitly permitted by JLS :
An implementation may decide to perform the conversion and concatenation in one step to avoid creating and then dropping the intermediate String object. To increase string re-concatenation performance, the Java compiler can use the StringBuffer class or a similar method to reduce the number of intermediate String objects created when evaluating an expression.
source share