Difference between String, StringBuffer and StringBuilder, stringbuffer
Directory:
1. Immutable String class
2. What should I pay attention to when using String?
3. Differences between String and StringBuilder
4. Differences between StringBuilder and StringBuffer
Immutable String class:
For this non-variability, you need to check the source code. There are several member variables in the String class:
Private final char [] value; // stores the String value
Private final int offset;
Private final int count;
They are all final types. They cannot be changed after you create a String, so this immutable refers to the immutable value of String.
What should I pay attention to when using String?
Because of the immutability of String, you should avoid creating a large number of String objects. for a large number of final String objects, a large amount of memory will be used temporarily, which is very valuable.
Differences between String and StringBuilder
StringBuilder does not create new String objects when performing String connection and other operations. Therefore, we recommend that you convert a String to StringBuilder before performing operations during String operations, which saves memory.
Difference between StringBuilder and StringBuffer
The difference between the two is that StringBuffer is thread-safe, and its methods are locked and mostly used in multithreading. It is recommended to use StringBuilder in a single thread, which will be faster.