An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1

It’s advised to use StringBuffer or StringBuilder to concatenate String objects. However, the specification said that a “+” operator is rewritten by the Java Compiler into StringBuilder.

https://stackoverflow.com/questions/4648607/stringbuilder-stringbuffer-vs-operator

Tags:

Categories:

Updated: