-2

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?

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

1 Answers1

4

What is the difference between + and concat()

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