Java --- StringBuffer and StringBuilder Concept in the case of the string does not change frequently

Concept

In the case of the string does not change frequently, we prefer String.

String "+" stitch may also be used to modify the string, the String str = "I" + "am" + "String" faster time efficiency, but with "+" String object splicing a plurality of space-consuming and executed inefficient (new objects, recycled object spend a lot of time), especially when the memory is no more after the reference object, JVM's GC began to work, will have an impact on system performance.

StringBuffer to solve a lot of string concatenation to generate a lot of problems when intermediate objects provided class, he is a thread-safe variable sequence of characters, similar to a string, but can be modified.

StringBuilder JDK 5 as the release of supplementary classes StringBuffer, StringBuffer is the equivalence class, but he was specially designed for the single-threaded.

StringBuffer

At any time, the StringBuffer containing certain character sequences, but the length and content of the sequence may be modified by a specific call process.
StringBuffer for the use of multi-threading is very safe, its methods are synchronized where necessary so that all operations for any particular instance is like occur in a particular sequence order, issued this method calling sequence consistent with each single-threaded of.
For the major operation StringBuffer append and insert methods, which have been subjected overload that can accept data in any form.
Each has a capacity of StringBuffer. As long as the length of the string in the sequence to not more than StringBuffer, it will not assign a new array of internal buffer space. If the internal buffer overflows, it will automatically expand the space.
StringBuilder

StringBuilder sequence is a string variable. This class provides the StringBuffer coexistence API, but these methods do not guarantee synchronization. This class is designed as an alternative to the StringBuffer, StringBuffer when those are single-threaded use (most often the case).
Examples of StringBuilder when using multiple threads is not safe. If you need to thread synchronization, it is recommended to use StringBuffer.
Method

Method, Significance type
the StringBuffer the append (int I) has many overloads, add String parameter represents the end of the buffer
StringBuffer insert (int offset, int i ) has many overloads, add parameters to the specified position indicates a String
char charAt ( int index) returns the specified sequence of char index value
StringBuffer delete (int start, int end ) removing the sequence neutron string of characters
int indexOf (String str) returns the index of the first occurrence of the specified substring
int lastIndexOf (String str) above, from the after the forward search
int length () returns the number of characters in buffer
StringBuffer replace (int start, int end , String str) character substring replace the specified parameter sub-string of characters
StringBuffer reverse () inverted sequence
void setCharAt (int index, char ch) the index of the character set i is CH
CharSequence subSequence in (int Start, int End) returns the substring of a new char sequences
String substring (int start, [int end]) returns the substring of a String
String toString () String to return the data sequence indicates
the same method StringBuilder and StringBuffer

Example

public class test{
public static void main(String args[]){

STR = new new StringBuilder StringBuilder ();
str.append ( "BuilderBuffer");
System.out.println (STR); BuilderBuffer //
// add characters to the StringBuilder with the append
str.insert (. 7, "and");
the System .out.println (STR); BuilderandBuffer //
// append a character is inserted into the StringBuilder
System.out.println (str.charAt (. 4)); // D
// return the index value char 4
System.out.println (str.indexOf ( "B")); // 0
System.out.println (str.lastIndexOf ( "B")); // 10
// search for the same character from the front and from the index value B does not return the same
System .out.println (str.length ()); // 16
// return character length StringBuilder
System.out.println (str.replace (7,10, "or")); // BuilderorBuffer
// parameter specifies replaced with location string
System.out.println (str.subSequence (7,9) instanceof CharSequence );to true //
// return char sequence substring
System.out.println (str.substring (9, 15)); // Buffer
// substring return String type
System.out.println (str.delete (7, 9)); // BuilderBuffer
// delete the specified character position
System.out.println (str.reverse ()); // reffuBredliuB
// reverse sequence
String str2 = str.toString ();
System.out.println (str2); reffuBredliuB //
// method call toString returns a String object
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31
32
Difference

String class is different and, subject StringBuffer StringBuilder class and can be modified a plurality of times, and does not generate new unused objects.

If you want to operate a small amount of data, use String. If a large number of string manipulation case, in the case of single-threaded, the string should be used to operate StringBuilder, multiple threads, should be used StringBuffer.

We in general should be more popular in the StringBuilder, and StringBuffer it supports the same operation, and faster than it is, because these methods in the implementation of non-synchronous.
----------------
Disclaimer: This article is CSDN blogger "zzZsleep7 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/zzZsleep7/java/article/details/105233861

http://www.zhijieketang.com/user/15697/about
http://www.zhijieketang.com/user/15698
http://www.zhijieketang.com/user/15698/about
http://www.zhijieketang.com/user/15699
http://www.zhijieketang.com/user/15699/about
http://c.bailitop.com/user/61995
http://c.bailitop.com/user/61995/about
http://user.show160.com/8651411
http://m.show160.com/user/pspace/Default.aspx?uid=8651411
https://www.meipian.cn/1i3x1s02
https://www.meipian.cn/1i3x08y3
http://www.djhaiba.com/index.php/26829/home
http://www.djhaiba.com/index.php/26832/home
https://www.nowcoder.com/library/book-list/4242
https://www.nowcoder.com/library/book-list/4243
https://www.nowcoder.com/library/book-list/4244
https://www.nowcoder.com/library/book-list/4245
https://www.nowcoder.com/library/book-list/4246

Guess you like

Origin www.cnblogs.com/dasdfdfecvcx/p/12622901.html