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.

Updated on 30-Jul-2019 22:30:21