1.String is the final type and cannot be inherited is immutable
StringBuffer and StringBuilder can be changed.
2. Speed stringbuilder>stringbuffer>string
StringBuffer is thread-safe
Stringbuidler is thread-insecure
StringS1=“ThisIsOnlyA "+ " simple " + "test" ; StringBuffer sb = new stringbuilder ( "this is only a ") . Append ( "simple" ) append ( "test"
Java compiler directly compiles the first statement above to:
String s2 = “This is only a”; String s3 = “ simple”; String s4 = “ test”; String s1 = s2 + s3 + s4;
3. Using policies
1) A small number of data operations can be string, single-threaded operation with Sringbuilder, multi-threaded operation with StringBuffer
2) do not use string "+" to splice, so the performance is very poor, you should use the following second:
Stringresult= ""; for(Strings: Hugearray) { result= result+ s ; } stringb Uilder sb = new stringbuilder (); for (string s : Hugearray) {sb. Append (s} string result = sb tostring ()
3) StringBuffer used in global variables; StringBuilder is used inside the method
3) generally use StringBuffer directly, as Builder's performance increases by up to 10%-15%, unless the acknowledgement is not running under multithreading and the performance bottleneck is on buffer.
The difference between String StringBuilder StringBuffer