Java.lang.StringBuilder Source Code Analysis

Source: Internet
Author: User

StringBuilder is a mutable sequence of character array objects that inherit from the Abstractstringbuilder abstract class. It is not guaranteed to be synchronous and designed to replace StringBuffer when this string cache is only used by a single thread. It is recommended to use it instead of stringbuffer because it is faster.

StringBuilder has its own capacity (character array), and when the capacity is not fully occupied, it is not necessary to apply for new space, and when the capacity is fully occupied, it will automatically expand.

 Public Final classStringBuilderextendsAbstractstringbuilderImplementsjava.io.Serializable, charsequence { PublicStringBuilder () {//constructor method simply calls the constructor of the parent classSuper(16); }         PublicStringBuilder (intcapacity) {//capacity Specifies the size of the capacitySuper(capacity); }     PublicStringBuilder (String str) {//STR Specifies the string that the initial container holds, and the capacity is str.length+16Super(Str.length () + 16);    Append (str); }     PublicStringBuilder (charsequence seq) { This(Seq.length () + 16);    Append (seq); }
StringBuilder overloads all the Append methods of the parent class to support adding any type of data to the container @Override
PublicStringBuilder Append (Object obj) {returnAppend (string.valueof (obj)); } @Override PublicStringBuilder Append (String str) {Super. Append (str); return This; } PublicStringBuilder Append (stringbuffer sb) {Super. Append (SB); return This; } @Override PublicStringBuilder Append (charsequence s) {Super. Append (s); return This; } PublicStringBuilder Append (charsequence s,intStartintend) { Super. Append (S, start, end); return This; } @Override PublicStringBuilder Append (Char[] str) { Super. Append (str); return This; } //... More append overloaded methods that implement all data types in StringBuilder append PublicStringBuilder Appendcodepoint (intcodepoint) {//This method uses Unicode encoding to append a character, such as ' a ' unicode=97,appendcodepoint (97) is equivalent to adding a to the end of a containerSuper. Appendcodepoint (codepoint); return This; } PublicStringBuilder Delete (intStartintend) {//delete End-start's substringSuper. Delete (start, end); return This; } PublicStringBuilder Deletecharat (intindex) {//delete the character of index subscriptSuper. Deletecharat (index); return This; } PublicStringBuilder Replace (intStartintend, String str) {//Use STR instead of End-start substringSuper. Replace (start, end, str); return This; }
Insert method implements inserting data anywhere in a string
PublicStringBuilder Insert (intIndexChar[] str,intOffset,intLen) { Super. Insert (index, str, offset, Len); return This; } //... More insert overloading Methods Public intindexOf (String str) {//Find the starting position of the substringreturn Super. indexOf (str); } //... More indexof overloaded methods to find the location of the substring PublicStringBuilder Reverse () {//reverse the string inside the containerSuper. reverse (); return This; } Publicstring toString () {//tostring is a string object that returns a new//Create a copy, don ' t share the array return NewString (value, 0, Count); } Private voidWriteObject (Java.io.ObjectOutputStream s)//writeobeject provides a serialization mechanismthrowsjava.io.IOException {s.defaultwriteobject (); S.writeint (count); S.writeobject (value); } Private voidReadObject (Java.io.ObjectInputStream s)//readobject provides deserialization mechanismthrowsjava.io.IOException, ClassNotFoundException {s.defaultreadobject (); Count=S.readint (); Value= (Char[]) s.readobject (); }}

StringBuilder internally maintains a character array, which performs various operations on the character array, and when it is necessary to perform frequent operations on the string, StringBuilder provides good performance, which does not require many objects to be created than to manipulate the string directly.

Java.lang.StringBuilder Source Code Analysis

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: info-contact@alibabacloud.com 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.