Java thousand ask _06 data structure (022) _stringbuffer/stringbuilder What's the Difference

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.