String, StringBuffer, StringBuilder source code analysis

String: The efficiency is the highest if short characters are spliced, such as String a="a"+"b"+"c"; the compiler will translate it into String a="abc"

But if it is String a = "a";

String b = "b";

String c = "c";

String d = a + b + c; is not as good as StringBuffer, because the underlying implementation is to create StringBuffer to append.

String is immutable because private final char value[]; initialization is final.

StringBuffer is thread-safe because its append method, delete method, replace method, etc. are all modified by synchronized.

StringBuilder does not have a synchronized modification method, so it is thread-unsafe.

In addition, StringBuffer and StringBuilder are initialized to a length of 16

super(16);

The expansion is 2 times the original length + 2

The append method, delete method, replace method, etc. ultimately call Ssytem.arraycopy for character changes (source array, source array starting position, destination array, destination array starting position, copy length)

There is no difference between subString and String, because the last is new String (source array, starting position, interception length)

Guess you like

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