First of all, the difference between string,stringbuffer
There are two main aspects, the first is the thread safety aspect, the second is the efficiency aspect
Thread Safety aspects:
string is not thread-safe, which means that when a string is shared by different threads, it needs to be synchronized, otherwise there will be different values for different threads, that is, the thread is unsafe
StringBuffer are thread-safe and are automatically synchronized when shared among different threads, and do not require manual synchronization.
Efficiency aspects:
Each time a string is changed, a variable is re-established, the changed value is given, and the JVM's GC mechanism is triggered after multiple changes, occupying resources
StringBuffer changes the original variable every time it changes, so it's not more efficient when you change it multiple times.
Second, the difference between StringBuffer and StringBuilder
StringBuilder is thread insecure, but is more efficient than stringbuffer when used in a single-threaded environment
Comprehensive:
The order of precedence for use should be, StringBuilder >stringbuffer>string
In Java, the difference between string,stringbuffer,stringbuilder