1.String: non-changing sequence of Unicode characters.
Pooling ideas, put the data that needs to be shared in a pool, and use a storage area to store some common resources to reduce the overhead of storage space.
In the string class, when created in literal value, go back to the string pool of Java method space, and if not, create a string object in the pool and return the address assigned to the object variable, if any, return the address of the string in the pool and assign the address to the object variable. If new, a string class object is created in the heap space and there is no such procedure.
String s1= "ABC"; // newly created, string constant pool is not in, then a string "abc" is created in the pool String s2= "ABC"; // "ABC" already exists in the string pool, and S2 will point to "ABC" instead of creating a new one. string s3=new string ("abc"); Open a new space directly in the heap, without going to the pool to find it.
Calling a method in any string does not alter the string itself unless it is re-copied.
2.StringBuffer: A sequence of Unicode characters that can be changed. Allows concurrent operations, which are thread-safe.
The string class is inefficient when connecting strings because the properties of the object it produces cannot be modified, and only new objects can be created when the connection string is made.
For many string connections, you should use the StringBuffer class, which optimizes efficiency by not having extra intermediate objects generated when strings are concatenated by objects of this class.
String str= "A" + "B" + "C" + "D"; // The " AB" generated in the string pool, "ABC" is obviously a waste of space for objects. String s=null; StringBuffer SB=new stringbuffer ("A"); Sb.append ("B"); Sb.append ("C"); Sb.append ("D"); S=sb.tostring ();
3.StringBulder: A sequence of Unicode characters that can be changed. Operations with StringBuffer, which do not support concurrent operations, are not thread-safe.
Java String,stringbuffer and Stringbulder learning notes