Explain the difference between StringBuilder and StringBuffer in Java.
StringBuilder
and StringBuffer
are both classes that represent mutable sequences of characters. The main difference is that StringBuilder
is not thread-safe, making it more efficient in single-threaded scenarios, while StringBuffer
is thread-safe, allowing safe use in multithreaded environments.