The difference between string, StringBuffer and StringBuilder

The difference between string, StringBuffer and StringBuilder

String, StringBuffer and StringBuilder in Java are string classes often used in programming. The difference between them is also a question often asked in the interview. Now to sum up, look at their differences and similarities.

1. Variable and immutable

The string class uses a character array to save strings, as shown below. Because there is a "final" modifier, you can know that the string object is immutable.

private final char value[];

String is an immutable object. Once it is created, its value cannot be modified The modification of an existing string object is to create a new object and save the new value

Both StringBuilder and StringBuffer inherit from abstractstringbuilder class. In abstractstringbuilder, character arrays are also used to save strings. As shown below, we can see that these two objects are variable.

char[] value;

StringBuffer: is a variable object. When modifying it, the object will not be re established like string. It can only be established through the constructor, such as StringBuffer sb = new stringbuffer();

He cannot be valued by the assignment symbol, For example, sb = "welcome to here!"// After the error object is created, memory space will be allocated in memory and a null will be initially saved When assigning values to StringBuffer, you can use its append method sb. append("hello");

2. Is multithreading safe

The object in string is immutable, which can be understood as a constant. Obviously, it is thread safe.

Abstractstringbuilder is the public parent class of StringBuilder and StringBuffer. It defines some basic string operations, such as expandcapacity, append, insert, indexof and other public methods.

StringBuffer adds a synchronization lock to the method or adds a synchronization lock to the called method, so it is thread safe. See the following source code:

public synchronized StringBuffer reverse() {

super . reverse();

return this ;

}

public int indexOf(String str) {

return indexOf(str,0); // There is a public synchronized int indexof (string STR, int fromindex) method

}

StringBuilder does not apply synchronization locks to methods, so it is non thread safe.

3. Common points between StringBuilder and StringBuffer

StringBuilder and StringBuffer have a common parent class abstractstringbuilder (abstract class).

One of the differences between abstract classes and interfaces is that some public methods of subclasses can be defined in abstract classes. Subclasses only need to add new functions and do not need to write existing methods repeatedly; The interface is only the declaration of methods and the definition of constants.

The methods of StringBuilder and StringBuffer will call the public methods in abstractstringbuilder, such as super append(...)。 Only StringBuffer will add the synchronized keyword to the method for synchronization.

Finally, if the program is not multithreaded, using StringBuilder is more efficient than StringBuffer.

Efficiency comparison string < StringBuffer < StringBuilder, but when string S1 = "this is only a" + "simple" + "test", string is the most efficient.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>