Click to enter _ more _java thousand ask
1. What is StringBuilder?
StringBuilder is a non-thread-safe mutable string class.
Java.lang.StringBuilder this variable character sequence class is 5.0 new. Inheriting from Abstractstringbuilder (most of the methods are implemented in this class) is not thread-safe and is likely to conflict when multiple threads modify an object at the same time.
Learn what a string looks here: what is a string
Part Abstractstringbuilder, StringBuilder Source:
Abstract class abstractstringbuilder implements appendable, charsequence { PublicAbstractstringbuilder append (Object obj) {returnAppend (string.valueof (obj)); } PublicAbstractstringbuilder Append (StringStr) {if(Str==NULL)Str="NULL";intLen =Str. Length (); Ensurecapacityinternal (Count+ len);Str. GetChars (0, Len, Value,Count);Count+ = Len;return This; }}
public final class stringbuilder extends abstractstringbuilder implements java . io . serializable , charsequence { public StringBuilder append (Object obj) {return append (string.valueof (obj)); } public StringBuilder append (String str) {super . Append (str); return this ; }}
2. What is StringBuffer?
StringBuffer is a thread-safe variable String class
Java.lang.StringBuffer inherits from Abstractstringbuilder (which is inherited from the class as well as StringBuilder, and some methods are implemented in that class), overriding most of its methods, without changing the logic to just change it to synchronized thread synchronization.
Part StringBuffer Source:
Public Final class stringbuffer extends abstractstringbuilder implements Java. io. Serializable, charsequence{ Public synchronizedStringBufferAppend(Object obj) {Super. Append (string.valueof (obj));return This; } Public synchronizedStringBufferAppend(String str) {Super. Append (str);return This; }}
3. What is the difference between stringbuffer/stringbuilder
As can be seen from the above concept, StringBuffer is thread-safe, and StringBuilder is not, StringBuilder may conflict in multiple threads.
So, when it comes to ensuring thread safety, you need to use stringbuffer, whereas in multi-threading, StringBuffer is worse than StringBuilder (synchronized is the equivalent of a thread jam).
Java q _06 data structure (022) _stringbuffer/stringbuilder What's the difference