Difference between StringBuilder and StringBuffer in Java

Java 8Object Oriented ProgrammingProgramming

StringBuilder is non-syncronized and thus is not threadsafe whereas StringBuffer is syncronized and threadsafe. But StringBuffer is slow in comparison to StringBuilder because of synchronization overhead.

StringBuffer should be used in the multithreaded environment whereas if only local variables are in scope or thread synchronization is not needed, StringBuilder is preferred.

Published on 07-Feb-2018 10:49:32