1. String class
The value of string is immutable, which results in a new string object being generated each time a string is manipulated, not only inefficient, but also a significant waste of limited memory space.
String a = "a"; Suppose a points to address 0x0001
A = "B";//re-assigns a point to address 0x0002, but "a" saved in the 0x0001 address still exists, but is no longer the point of a, a has pointed to other addresses.
So the operation of string is to change the assignment address instead of changing the value operation.
2. StringBuffer is a variable class, and thread-safe string manipulation class, and any manipulation of the string it points to does not produce new objects. Each StringBuffer object has a certain buffer capacity, and when the string size does not exceed the capacity, no new capacity is allocated and the capacity is automatically increased when the string size exceeds the capacity.
StringBuffer buf=new StringBuffer (); Assigning a 16-byte long character buffer
StringBuffer buf=new StringBuffer (512); Assigning a 512-byte long character buffer
StringBuffer buf=new StringBuffer ("This is a test")//The string is stored in the buffer, and a 16-byte empty buffer is reserved behind it.
3.StringBuffer
StringBuffer and StringBuilder class functions are basically similar, the main difference is that the StringBuffer class method is multithreaded, secure, and StringBuilder is not thread-safe, in contrast, The StringBuilder class will be slightly faster. The StringBuffer and StringBuilder classes should be used for strings that frequently change values.
4. Thread Safety
StringBuffer Thread Safety
StringBuilder thread is not secure
5. Speed
In general, speed from fast to slow: stringbuilder>stringbuffer>string, this comparison is relative, not absolute.
6. Summary
(1). If you want to manipulate a small amount of data with = String
(2). Operating a large amount of data under a single-threaded operation string buffer = StringBuilder
(3). Multi-threaded operation string buffers operation of large amounts of data = StringBuffer
Full explanation of StringBuilder, StringBuffer, and string relationships