What is the difference between +
and concat()
? Why is concat()
memory efficient? So, why is +
operator better in performance at small number of additions of Strings, while concat()
is better at memory usage and performance at large number of concatenation of Strings?
Asked
Active
Viewed 59 times
-2

Mark Rotteveel
- 100,966
- 191
- 140
- 197

Abhishek S
- 1
- 1
-
7"why is concat() memory efficient ?" Citation needed. – Sören Aug 15 '23 at 16:29
1 Answers
4
What is the difference between
+
andconcat()
There isn't a meaningful one. (In older versions of Java, concat
was more efficient when you started with two strings, but that certainly hasn't been true since Java 9 as a result of JEP 280.)
Why is
concat()
memory efficient?
It isn't any more efficient.
does doing concat store the String in the String pool only and for + does it store in heap and pool?
No. There is no difference regarding the pool. (And the string pool is never worth worrying about.)

Mark Rotteveel
- 100,966
- 191
- 140
- 197

Louis Wasserman
- 191,574
- 25
- 345
- 413
-
There is one difference: Using `+` between string literals (or final variables referencing string literals) will produce a single string at compile time, which will be put in the string pool. – Mark Rotteveel Aug 17 '23 at 09:26