1//The similarities and differences of String, StringBuffer, StringBuilder
Same: They are all stored in char[] at the bottom
String, immutable character sequence (multi-threaded is also safe)-"Why? Because the String
class is final
, it means that it cannot be inherited.
StringBuffer, variable character sequence, thread is safe, low efficiency;
StringBuilder, variable character sequence, newly added in jdk5.0. Threads are not safe and efficient.
Why are StringBuffer and StringBuilder variable character sequences? "abc" occupies 3 characters in String, but StrignBuffer and StringBuildr are not only 3 characters, there are many empty characters behind. There is a row there.
If you need to make a lot of modifications to the string, you should choose to use the StringBuffer & StringBuilder class.