What's the difference between javaString, StringBuilder and StringBuffer?
This article introduces what is the difference between javaString, StringBuilder and StringBuffer. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
1. What they have in common:
All three are string classes. Both StringBuilder and StringBuffer inherit the AbstractStringBuilder class, and storage strings are stored using arrays.
two。 Difference:
A. the array used by the String class to store strings has been modified by final. When adding modified characters to the String object, you need to re-new a character array and copy the original value to the new character array, so when the value of the string needs to be modified frequently, you need to consider using StringBuilder or StringBuffer to deal with it.
Public final class String implements java.io.Serializable, Comparable, CharSequence {private final char value [];}
B.StringBuffer and StringBuilder also use character arrays to store strings, but do not use final to decorate them; methods defined by StringBuffer are decorated with synchronized, which means that StringBuffer class methods are thread-safe, while StringBuilder methods are not decorated, and the common method for both is append ().
3. Use the scene:
Consider using String when the string value does not change. When the string value is variable, consider using StringBuilder or StringBuffer. When thread safety is required, consider using StringBuffer.
So much for sharing the differences between javaString, StringBuilder and StringBuffer. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.