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 <elh...@ibiblio.org>
wrote:

> On Sun, Dec 15, 2024 at 2:00 PM Peter Burka <pe...@quux.net> 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
> elh...@ibiblio.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>
>

Reply via email to