A daily reading:

Go to work to go to work, go to work to go to work the next day, day after day, panic zhang Zhang, but also for a few pieces of silver. But it happened that a few pieces of silver, pressure broken people's shoulders, this is life. I like a paragraph very much: life is not easy, everyone wants different, their positions are different. Don't cultivate yourself in others' mind, don't force others in your own mindCopy the code

String

String is a very fundamental and important class in the Java language that provides the basic logic for constructing and managing strings.

Because strings are frequently used in Java, Java introduced a string constant pool to prevent a system from producing a large number of strings. When creating a string, check whether there are any identical string objects in the constant pool. If there are, do not create the object reference directly from the pool. If not, create a new string object and return the object reference. And put the new object into the pool. However, String objects created through the new method do not check the constant pool, but instead create a new object in the heap or stack. Objects are not pooled; The above principle only applies if you assign a String reference by a direct quantity

For example: String STR = “123”; It goes into the constant pool by direct quantity assignment

String STR = new String (” 1230 “); By assigning to new, it’s not in the constant pool

Note: String provides the inter () method, which, when called, returns the String object in the constant pool if it contains a String equal; otherwise, the String object is added to the pool and a reference to the object in the pool is returned.

String features:

[A] Immutable, meaning that once A String is generated, it cannot be changed again. Immutable is mainly used when an object needs to be shared by multiple threads and is frequently accessed, the synchronization and lock wait time can be ignored, thus greatly improving performance

[B] Optimization for constants. When two strings have the same value, they reference only the same copy in the constant pool. This technique can save a lot of memory when a string is repeated.

StringBuffer

You can concatenate strings with append and Add, essentially a thread-safe sequence of modifiable characters. Unless there are thread-safety concerns, stringBuilder is preferred for performance purposes

stringBuilder

It has the same properties as stringBuffer, minus the thread-safety issues.

Knowledge extension

String is a typical implementation of the Immutable class, which natively ensures thread-safe underlying data because it cannot make any changes to internal data. This traversal is even present in constructors, because immutable objects do not require additional data to be copied.

StringBuffer thread safety is achieved by adding the synchronized keyword to various methods of modifying data,

Both StringBuffer and StringBuilder base classes make use of modifiable (char, JDk9, and later byte) arrays. Both classes inherit AbstractStringBuilder and contain basic operations. The difference ultimately comes down to whether or not synchronized is added.