StringBuffer
Mutability: Unlike String, StringBuffer objects are mutable, meaning you can modify the content without creating new objects.
Thread-Safety: StringBuffer is thread-safe because all of its methods are synchronized. This means that multiple threads can use a StringBuffer object without causing data corruption, but it also means it is slower in a single-threaded environment compared to StringBuilder.
Performance: StringBuffer is more efficient than String when you need to perform many modifications to the string content, but it's slower than StringBuilder due to its thread-safety guarantees.
eg: StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
StringBuilder
Mutability: Like StringBuffer, StringBuilder is mutable and allows modifications to its content without creating new objects.
Thread-Safety: StringBuilder is not thread-safe. It does not provide synchronized methods, making it faster than StringBuffer in a single-threaded environment.
Performance: StringBuilder is generally preferred over StringBuffer for single-threaded programs due to its better performance.
eg: StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");