1. Overview
StringBuffer is actually a container for storing characters
String characteristics: strings are constants; they cannot be changed after they are created.
Once the string has changed, a new object is created immediately.
Note: the contents of the string are not suitable for frequent modification, because once the modification creates a new object immediately, but the contents of the string need to be modified repeatedly, it is recommended to use the string buffer Class (StringBuffer)
written question: when creating an object using the StringBuffer parameterless constructor, what is the default initial capacity? How many times does it increase automatically if the length is not enough? StringBuffer The underlying dependent character array to store character data, the default initial capacity of the string array is 16, if the character array is not long enough to automatically grow by 1 time times
2. The difference between StringBuffer and Stringbuillder
2.1, the same point
1. Two classes are string buffer classes
2. The methods of the two classes are the same
2.2, different points
1.StringBuffer is thread-safe, operating efficiency is low, stringbuillder is non-thread-safe, operation efficiency is high.
2.StringBuffer appeared jdk1.0, Stringbuillder was jdk1.5.
Recommended Use: Stringbuillder because of its high operating efficiency.
3. The difference between StringBuffer and string
1. A string object is an immutable object, and whenever we manipulate a string, it overrides the new object to hold the value
2, StringBuffer object once instantiated, only for this one object operation
1 Packagecom.bw.string;2 3 Public classDemo4 {4 Public Static voidMain (string[] args) {5String str= "ABC";6StringBuffer sb=NewStringBuffer ("abc"));7Runtime runtime=runtime.getruntime ();8 LongStarttime=System.currenttimemillis ();9 Longstartfreememory=runtime.freememory ();Ten for(inti=0;i<10000;i++){ One //str+=i;//operating Result: Operation time: 492ms, memory consumption: 8161KB ASb.append (i);//operating Result: Operation time: 10ms, memory consumption: 147KB - //The result of re-operation is also: StringBuffer is more efficient than string - } the Longendfreememory=runtime.freememory (); - LongEndtime=System.currenttimemillis (); - -SYSTEM.OUT.PRINTLN ("Operation time:" + (Endtime-starttime) + "MS," + "Memory Consumption:" + (startfreememory-endfreememory)/1024+ "KB"); + } -}4, StringBuffer under the Common method (function) 4.1, the behavior of the container
4.1.1, Increase
Append (Boolean B): can add any type of data to the container
Insert (int offset, Boolean b): Specifies the inserted index value, inserting the corresponding content
4.1.2, deleting
Delete (int start, int end): deletes the corresponding content based on the specified start and end index values
deletecharat (int index): deletes one character based on the specified index
4.1.3, modification
replace (int start, int end, String str):
reverse ()
setcharat (int index, char ch):
substring (int start, int end):
ensurecapacity (int minimumcapacity):
4.1.4, viewing
indexOf (String str, int fromIndex) :
lastIndexOf (String str, int fromIndex) :
capacity () :
Length () :
charAt (int index) :
toString ():
5. Example
|
Original Dshore
originating from:http://www.cnblogs.com/dshore123/
Welcome reprint, reprint must explain the source. ( If this article is useful for you, you can click on the lower right corner of the recommendation , thank you!) ) |
Java Foundation StringBuffer Buffering class