I'm not sure Sun/Oracle ever formally specified this behavior, but as a JVM
engineer since Java 0.98 I can attest to it. This behavior is also
described in JEP-280: "Currently javac translates String concatenation into
StringBuilder::append chains" (referring to Java 8).

The advantages of using StringBuilder explicitly, that I can think of, are:

* No risk of accidentally constructing multiple StringBuilders, e.g. (strA
+ strB) + (strC + strD)
* Ability to explicitly set the right capacity, e.g. new StringBuilder(new
size)

Peter


On Sun, Dec 15, 2024, 11:03 AM Elliotte Rusty Harold <[email protected]>
wrote:

> On Sun, Dec 15, 2024 at 2:00 PM Peter Burka <[email protected]> wrote:
> >
> > Before Java 9, javac always generated StringBuilder calls for string
> > concatenation (or StringBuffer prior to Java 2). Using + is less verbose,
> > generates the same code, is more readable, and, when we do finally bump
> the
> > compile target, will generate better code.
> >
>
> reference?
>
>
> --
> Elliotte Rusty Harold
> [email protected]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to