The difference between StringBuilder and StringBuffer

The difference between StringBuilder and StringBuffer, and the difference between StringBuilder and String.

1) StringBuilder is efficient and thread-insecure, StringBuffer is inefficient and thread-safe.

2) String is immutable string, StringBuilder is mutable string. Why there is such a difference, you can go deep into the source code to analyze, such as the private final char value[] and other methods in the String class.

3) If it is a simple statement that a string does not have too many subsequent operations, either String or StringBuilder can be used. If the characters are frequently added and deleted, or the length of the characters is dynamically changed in the loop, it should be Use StringBuilder. Using String will generate redundant strings and take up memory space.

Guess you like

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