This is the 18th day of my participation in the August Genwen Challenge.More challenges in August
Include a column
Spring Boot Quick start
Java full stack architect
preface
In the previous article, you were familiar with Java String operations. In this article, you work with the StringBuffer and StringBuilder classes, which are similar in type to String. Of course, using StringBuffer and StringBuilder allows for frequent updates without the need to create new objects. Let’s begin.
Portal: [basics] Java String operations, have you learned? More challenges in August
The inheritance structure of String, StringBuffer, and StringBuilder is as follows:
classDiagram
charsequence <|-- Stirng
charsequence <|-- AbstractStringBuilder
charsequence : interface
Appendable <|-- AbstractStringBuilder
Appendable : interface
AbstractStringBuilder <|-- StringBuilder
AbstractStringBuilder <|-- StringBuffer
StringBuffer
A StringBuffer is mutable and thread-safe string manipulation class, and any operation on the string it points to does not create a new object. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed its capacity, no new capacity is allocated; when the string size exceeds its capacity, it is automatically increased.
advantages
- Variable length
- thread-safe
- Multithreaded operation
disadvantages
- StringBuffer is less efficient than StringBuilder.
StringBuilder
StringBuilder is mutable, and thread-unsafe string manipulation class; any operation on the string it points to does not generate a new object. Each StringBuilder object has a certain buffer capacity, and no new capacity is allocated when the string size does not exceed its capacity, and it is automatically increased when the string size exceeds its capacity.
advantages
- Variable length
- Threads are not safe
- Single thread operation
disadvantages
- StringBuilder Efficiency A StringBuffer is less efficient than a StringBuffer.
StringBuffer is a common method
append
Append (String s) appends the specified String to this character sequence.
reverse
Reverse () replaces this character sequence with its reversed form.
delete
Delete (int start, int end) removes characters from substrings of this sequence.
insert(int offset, int i)
Insert (int offset, int I) inserts the string representation of the int argument into the sequence.
insert(int offset, String str)
Insert (int offset, String STR) inserts the String of the STR argument into the sequence.
replace(int start, int end, String str)
Replace (int start, int end, String STR) replaces the characters in the substrings of this sequence with the characters in the given String.
StringBuffer is a common method
capacity()
Int capacity() returns the current capacity of an int value.
charAt(int index)
Char charAt(int index) returns the char value at the specified index in this sequence.
ensureCapacity(int minimumCapacity)
EnsureCapacity (int minimumCapacity) ensures that the capacity is at least equal to the specified minimum.
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
GetChars (int srcBegin, int srcEnd, char[] DST, int dstBegin) copies characters from this sequence into destination character array DST, with no return value.
indexOf(String str)
Int indexOf(String STR) returns the indexOf the specified substring in the String for the first occurrence.
indexOf(String str, int fromIndex)
Int indexOf(String STR, int fromIndex) returns the first occurrence of the specified substring index in the String starting at the specified index.
lastIndexOf(String str)
Int lastIndexOf(String STR) returns the index of the specified substring appearing at the rightmost in this String.
lastIndexOf(String str, int fromIndex)
Int lastIndexOf(String STR, int fromIndex) Mandatory String Last position of the neutron String in an object.
length()
Int length() returns the length(number of characters).
setCharAt(int index, char ch)
Void setCharAt(int index, char ch) sets the character at the given index to ch.
setLength(int newLength)
Void setLength(int newLength) Sets the length of a character sequence.
subSequence(int start, int end)
CharSequence subSequence(int start, int end) returns a new sequence of characters that is a subSequence of this sequence.
substring(int start)
String subSTRING (int start) Returns a new String containing the character subsequence currently contained in this character sequence.
substring(int start, int end)
String subString (int start, int end) returns a new String containing the character subsequence currently contained in this sequence.
toString()
String toString() returns a String representation of the data in this sequence.
conclusion
- String: immutable character sequence
- Stringbuffer: Mutable character sequences, inefficient, thread-safe
- String Builder(JDK1.5): Variable character sequences, high efficiency, thread unsafe
Stringbuilder and String Buffer are very similar in that they both represent mutable sequences of characters, and the method is the same. Repeated operations to change the contents of a String can result in a large number of duplicate String objects remaining in memory, which is inefficient. If such operations are placed in a loop, the performance of the program can be severely affected.
About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.
Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.