1.

What is the difference between StringBuffer and StringBuilder?

Answer»

STRINGBUFFER and StringBuilder expose the same kind of APIs to build a String and both are mutable classes. There is a big difference in them, though. StringBuffer is thread-safe which means it can be used as a shared object among MULTIPLE THREADS. On the other hand, StringBuilder is not thread-safe and should not be allowed to be MODIFIED by multiple threads without proper synchronization techniques. That’s why StringBuilder is faster than StringBuffer. In the scenario where we NEED to build a String object local to a method or local to a particular thread, we should prefer StringBuilder over StringBuffer. 



Discussion

No Comment Found