1

I came across this problem with a light weight application I am developing. The StringBuilder seems to be more efficient with the memory usage.

  1. why is it so?
  2. where else the use of StringBuilder is better than a good old String?

Thank you for giving me more details

william.eyidi
  • 2,315
  • 4
  • 27
  • 39

1 Answers1

1

StringBuilder uses less memory upon concatenation (because it doesn't copy both strings), which means that if you plan to make many concatenations (in a loop!) you'd better use StringBuilder.

Usually the comparison of performance is not done between StringBuilder and String but between StringBuilder and StringBuffer. The latter is synchronized which is "safer" but also much slower, hence again, the preference usually tends towards StringBuilder when you're not concerned about concurrency issues and you want optimal performance.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129