strings concatenation (string concatenation)
Compiler optimizations before string answer = Integerpart + "." + mantissa;//compiler optimized string answer = new StringBuilder (Integerpart). Append (".") . Append (Mantissa). toString ();
Because the compiler optimizes the concatenation of strings, using string concatenation in the same statement has no negative effect on performance. Just because the compiler is behind the scenes, it is not necessary to use StringBuilder instead of the "+" operator for string stitching in any scenario.
However, when the concatenation of strings is done by multiple statements (or loops) , there is a problem:
compiler optimization before string answer = Integerpart;answer + = "."; Answer + = mantissa;//Compiler optimized String answer = new StringBuilder (Integerpart). toString (); answer = new StringBuilder (answer) . Append ("."). ToString (); answer = new StringBuilder (answer). Append (Mantissa). ToString ();
In code that is optimized by the compiler, the middle string object and the StringBuilder object are virtually unwanted. At this point, you can consider merging the above stitching statements into a single stitching statement. or explicitly use the StringBuilder object directly.
Summarize
- When string concatenation is done using a single statement, performance is equivalent to using StringBuilder.
- Consider merging these statements or explicitly using StringBuilder when you are using multiple statements to complete string stitching.
[Java Performance] string stitching considerations