1. String
The array used to store characters is declared as final, so it can be assigned only once and cannot be changed. This results in a New String object generated for each String operation, which is not only inefficient, but also wastes a lot of limited memory space.
String a = "a"; // assume a points to the address 0x0001.
A = "B"; // After the value is re-assigned, a points to address 0x0002, but the "a" stored in address 0x0001 still exists, but it is no longer pointed to by, a has pointed to another address.
Therefore, the String operation changes the value assignment address rather than the value change operation. Any change to the String object does not affect the original object. Any change operation will generate a new object.
2. StringBuffer
StringBuffer is a class that exists since JDK1.0. It is like a String class, but its internal members can be modified.
StringBuffer is a variable class, andThread-safeString operation class. Any operation on the string to which it points will not produce new objects. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity is allocated. When the string size exceeds the capacity, the capacity is automatically increased.
The main operations on StringBuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer.The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point.For example, if z references a string buffer object whose current content is "start", this method calls z. append ("le") causes the string buffer to contain "startle", while z. insert (4, "le") will change the string buffer to include "starlet ".
3. StringBuilder
StringBuilder is a variable character sequence and is added to JDK. This class provides an API compatible with StringBuffer,Synchronization not guaranteed. This class is designed as a simple replacement of StringBuffer, used when the string buffer is used by a single thread (this is common ).
It provides the same interface as StringBuffer.
Similarities: they inherit the same parent class: Abstract StringBuilder
They implement the same interfaces: java. io. Serializable, CharSequence
They basically provide the same external method.
Difference: The StringBuffer class is thread-safe, and all the public methods provided by StringBuffer are basically synchronized. The StringBuilder class is not thread-safe and its method is not modified by the synchronized lock. In comparison, the StringBuilder class is slightly faster.
4. Summary
If you want to operate on a small amount of data, use String
Use StringBuilder to operate a large amount of data in a single-thread String Buffer
Multi-threaded operations on a large amount of data in the string buffer using StringBuffer
If a string variable is defined in the method, in this case, only one thread may access it and there is no unsafe factor, use StringBuilder. If you want to define a member variable in the class, and the instance object of this class willUse StringBuffer in a multi-threaded Environment.
String is an unchangeable object. Therefore, a new String object is generated every time the String type is changed, and the pointer is directed to the New String object, therefore, it is best not to use a String that often changes the content, because every time an object is generated, it will affect the system performance, especially when there are more referenced objects in the memory, the GC of JVM will start to work, and the performance will decrease.
Do not use the "+" of the String class for frequent splicing, because such performance is very poor, you should use the StringBuffer or StringBuilder class, this is an important principle in Java optimization.