Because strings are immutable, string concatenation causes the creation of a string. those. "A" + "B" + "C" causes the creation of "AB", then "ABC". If you perform multiple concatenations, it will always be more efficient to use a StringBuilder (rather than an old, but Api-like StringBuffer, which has the cost of synchronization)
If you have one string concatenation, the compiler will do it for you, if possible, that is, if you compile "A" + "B" + "C", you will most likely see if you decompile something sort of like a new StringBuilder ("A"). append ("B"). append ("C"). toString (). However, the compiler cannot optimize multiple concatenation of strings over several lines - i.e. If you have multiple concatenation lines, you'll see something like the above on each line, including the optional creation of a StringBuilder. Itβs better to do it manually.
You can verify this yourself by collecting a simple example and decompiling.
source share