0

My question is that many documentation over mutable strings, I have read that StringBuilder gives faster result as compare to StringBuffer. People gives reason that, StringBuilder is not synchronized and StringBuffer is synchronized, but it gives same result even with single thread. Please give me the valid reason for that.

Vatsal Jagani
  • 83
  • 1
  • 8
  • Synchronization can have a performance penalty regardless of lock contention. See also [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – shmosel Mar 17 '17 at 19:09
  • If you look at more than just the top answer to [Difference between StringBuilder and StringBuffer](http://stackoverflow.com/q/355089/5221149), you'll find the answers to your question. Unless you're really asking this: [Why are synchronize expensive in Java?](http://stackoverflow.com/q/1671089/5221149) – Andreas Mar 17 '17 at 19:14
  • Can you elaborate on "it gives same result even with single thread"? Showing your code/work on the test case(s) you are running might help you get more pertinent answers. – sumitsu Mar 17 '17 at 19:14
  • Don't use `StringBuffer`. There's really no use case for it. – Lew Bloch Mar 17 '17 at 19:50

0 Answers0