Detailed interpretation of abstractstringbuilder class source code

When looking at the source code of StringBuffer and StringBuilder, I found that both inherit abstractstringbuilder, and many methods are directly the methods of abstractstringbuilder, the parent class of super. Therefore, I decided to look at the source code of abstractstringbuilder first, and then StringBuffer and StringBuilder

Location: Java Lang package

Declaration: abstract class abstractstringbuilderexamples appendable, charsequence

Abstractstringbuilder class is decorated with abstract, so it can't be instantiated.

The abstractstringbuilder class has two subclasses: StringBuilder and StringBuffer.

field

constructor

1. Parameterless constructor

2. When creating the object of abstractstringbuilder implementation class, specify the buffer size as capacity.

When the child class StringBuilder or StringBuffer is instantiated, the constructor is called in the constructor.

Expansion capacity

void expandCapacity(int minimumCapacity)

This method has package access rights. Multiple methods in the class will call this method to expand the capacity when the capacity is insufficient.

Source code:

Assign the buffer length plus 1 by 2 to the variable newcapacity, and then compare this value with the specified value to determine the larger value as the new capacity of the buffer; Then we call the copyof method of the Arrays class. This method creates a new array, and then copies all the characters in the original array into the new array.

ensureCapacity(int minimumCapacity)

Ensure that the capacity is at least equal to the specified minimum value. If the current capacity is less than the specified value, a new array is created. The capacity of the new array is twice the specified value plus 2; If the current capacity is not less than the specified value, it will not be processed directly.

Source code:

Test:

method

The codepointat method uses character Codepointatimpl (value, index, count)

The getchars method is implemented using system Arraycopy() method

The append method involves the implementation of the ensurecapacityinternal () method and the getchars () method

Arrays. Is used Copyof()

Arrays. fill(value,count,newLength,‘\0'); Copy between strings

Delete() only changes the size of the string and does not actually delete the string

Learn to use system flexibly Arraycopy() method

summary

The above is all about the detailed interpretation of the source code of abstractstringbuilder class in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>