String,StringBuffer,StringBuilder

String:

· String constants , thread- safe .

· The String class is a final class that stores strings through a char array .

· String objects cannot be changed after they are created . Every time the String type is changed, a new String object is actually generated, and then the pointer is pointed to the new String object. Because the original String object has no reference to it, it will be garbage collected by the JVM (GC). to be recycled. So the execution speed is relatively slow .

· Optimization when instantiating strings: String constant pool . When creating a string constant, the JVM will first check the string constant pool. If the string already exists in the string constant pool, it will directly return the instance reference in the constant pool; otherwise, it will instantiate the string and put it in the constant pool. into the constant pool.

·

StringBuffer:

· String variables .

· It is thread-safe . If a StringBuffer object is used by multiple threads in the string buffer, many methods in StringBuffer can carry the synchronized keyword, so the thread is guaranteed to be safe.

· The capacity of StringBuffer is initialized to 16 characters, which means the default capacity is 16 characters. When StringBuffer reaches its maximum capacity, it will increase its capacity to twice the current capacity plus 2, that is ( new capacity=original capacity*2+2 ).

· Applicable to a large number of operations in the character buffer under multi-threading.

· StringBuffer > String in most cases .

·

StringBuilder:

· String variables .

· is not thread safe .

· Suitable for a large number of operations in the character buffer under a single thread.

· StringBuilder > StringBuffer in most cases .

·

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324590919&siteId=291194637