Earlier in Java, it was known that there was a class called StringBuffer, which was used to stitch longer strings. After you go to C #, there is also a class-like function called StringBuilder, shorthand is SB, very good to remember.
Then later transferred back to Java, and found that Java also has a StringBuilder, so I was curious why stringbuffer after the launch of the StringBuilder.
The original Java StringBuilder (as C #) is not thread-safe, and earlier StringBuffer has certain thread-safe properties. Of course, the introduction of StringBuilder, mainly because it is not necessary in the context of multithreading to use.
Common StringBuilder (or StringBuffer) use cases are:
Public String toString () {return
new StringBuilder ()
. Append ("Name:" + name)
. Append ("foo:" + foo)
. AP Pend ("bar:" + bar)
. toString ();
In this case, StringBuilder is not a class member, it is only a local variable, there is no problem of multithreading.
As a result, the introduction of StringBuilder brings a very large performance boost, and there is no security problem ...