String.concat is faster than the + operator if you are concatenating two strings... Although this can be fixed at any time and may even have been fixed in java 8 as far as I know.
The thing you missed in the first post you referenced is that the author is concatenating exactly two strings, and the fast methods are the ones where the size of the new character array is calculated in advance as str1.length() + str2.length(), so the underlying character array only needs to be allocated once.
Using StringBuilder() without specifying the final size, which is also how + works internally, will often need to do more allocations and copying of the underlying array.
If you need to concatenate a bunch of strings together, then you should use a StringBuilder. If it's practical, then precompute the final size so that the underlying array only needs to be allocated once.
Answer from Matt Timmermans on Stack Overflowjava - Most efficient way to concatenate Strings - Stack Overflow
dart - Optimizations of "+" operand for strings in JDK 17 - Stack Overflow
java - String concatenation performance: + vs StringBuilder - Stack Overflow
String Concatenation with (+)
Imagine a class:
import java.util.UUID;public class StringThing {public static void main(String[] args) {var x= UUID.randomUUID().toString();var y=UUID.randomUUID().toString();var z=x+y;System.out.println(z);}}
Running javap -c StringThing gives:
Compiled from "StringThing.java"public class StringThing {public StringThing();Code:0: aload_01: invokespecial #1 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: invokestatic #7 // Method java/util/UUID.randomUUID:()Ljava/util/UUID;3: invokevirtual #13 // Method java/util/UUID.toString:()Ljava/lang/String;6: astore_17: invokestatic #7 // Method java/util/UUID.randomUUID:()Ljava/util/UUID;10: invokevirtual #13 // Method java/util/UUID.toString:()Ljava/lang/String;13: astore_214: aload_115: aload_216: invokedynamic #17, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;21: astore_322: getstatic #21 // Field java/lang/System.out:Ljava/io/PrintStream;25: aload_326: invokevirtual #27 // Method java/io/PrintStream.println:(Ljava/lang/String;)V29: return}
Note line 16. This is from Java 17.
More on reddit.comVideos
String.concat is faster than the + operator if you are concatenating two strings... Although this can be fixed at any time and may even have been fixed in java 8 as far as I know.
The thing you missed in the first post you referenced is that the author is concatenating exactly two strings, and the fast methods are the ones where the size of the new character array is calculated in advance as str1.length() + str2.length(), so the underlying character array only needs to be allocated once.
Using StringBuilder() without specifying the final size, which is also how + works internally, will often need to do more allocations and copying of the underlying array.
If you need to concatenate a bunch of strings together, then you should use a StringBuilder. If it's practical, then precompute the final size so that the underlying array only needs to be allocated once.
What I understood from others answer is following:
If you need thread safety, use StringBuffer
If you do not need thread safety:
If strings are known before hand and for some reasons multiple time same code needs to be run, use '+' as compiler will optimize and handle it during compile time itself.
if only two strings need to be concatenated, use concat() as it will not require StringBuilder/StringBuffer objects to be created. Credits to @nickb
If multiple strings need to be concatenated, use StringBuilder.
Hello,
I was checking StringBuilder and StringBuffer classes and read that String "+" operator uses one of them. I could not find when and why and how it decides to use which one. Did anybody got curious and found the answer before?
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#:~:text=The%20Java%20language,Language%20Specification.
Here is the link from Java docs.
Imagine a class:
import java.util.UUID;public class StringThing {public static void main(String[] args) {var x= UUID.randomUUID().toString();var y=UUID.randomUUID().toString();var z=x+y;System.out.println(z);}}
Running javap -c StringThing gives:
Compiled from "StringThing.java"public class StringThing {public StringThing();Code:0: aload_01: invokespecial #1 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: invokestatic #7 // Method java/util/UUID.randomUUID:()Ljava/util/UUID;3: invokevirtual #13 // Method java/util/UUID.toString:()Ljava/lang/String;6: astore_17: invokestatic #7 // Method java/util/UUID.randomUUID:()Ljava/util/UUID;10: invokevirtual #13 // Method java/util/UUID.toString:()Ljava/lang/String;13: astore_214: aload_115: aload_216: invokedynamic #17, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;21: astore_322: getstatic #21 // Field java/lang/System.out:Ljava/io/PrintStream;25: aload_326: invokevirtual #27 // Method java/io/PrintStream.println:(Ljava/lang/String;)V29: return}
Note line 16. This is from Java 17.
It's version dependent and also depends on what variables you add in addition to the Strings. Here is an explanation: https://www.baeldung.com/java-string-concatenation-invoke-dynamic