Home  >  Article  >  Java  >  What is the difference between string, stringbuffer and stringbuilder?

What is the difference between string, stringbuffer and stringbuilder?

王林
王林Original
2020-05-11 10:28:4517062browse

What is the difference between string, stringbuffer and stringbuilder?

The differences are as follows:

1. Once the content of the String class is declared, it cannot be changed. What changes is only the pointer to its memory, while the object content of the StringBuffer class is Can be changed.

2. For StringBuffer, object instantiation cannot be completed directly through assignment like String, but must be completed through the constructor method.

3. StringBuffer does not generate new objects when processing strings, and is better than the string class in terms of memory usage. Therefore, in actual use, if you often need to modify a string, such as insertion, deletion and other operations, it is more suitable to use StringBuffer.

4. The biggest difference between StringBuilder and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously).

5. StringBuilder has a speed advantage compared to StringBuffer, so it is recommended to use the StringBuilder class in most cases. However, when the application requires thread safety, the StringBuffer class must be used.

The above is the detailed content of What is the difference between string, stringbuffer and stringbuilder?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn