Java Fundamentals (StringBuffer)----and StringBuilder

Source: Internet
Author: User

StringBuffer string Buffers:

Constructs a string buffer with no characters, with an initial capacity of 16 characters.
Characteristics:
1: You can modify the contents of the string.
2: is a container.
3: Is variable-length.
4: Any type of data can be stored in the buffer.
5: Eventually it needs to become a string.

A container usually has some fixed methods:
1, Add.
StringBuffer Append (data):append data to the buffer. Append to tail.
StringBuffer Insert (Index,data):inserts data at the specified location.
2, delete.
StringBuffer Delete (start,end);remove elements from the start to End-1 range
StringBuffer Deletecharat (index);deletes the element at the specified position
Sb.delete (0,sb.length ());/empties the buffer.
3, modify.
StringBuffer replace (start,end,string);Replace start to end-1 with string
void Setcharat (Index,char);replace the character at the specified position
void SetLength (LEN);Place the original string as a string of the specified length
4, find. (Cannot find return-1)
int indexOf (string); Returns the index at which the specified substring first appears in this string.
int indexOf (String,int fromIndex); Find a string starting at the specified position
int lastIndexOf (string); Returns the index of the rightmost occurrence of the specified substring in this string.
int lastIndexOf (String,int fromIndex); Reverse search starting from the specified index
5, gets the substring.
String substring (start); Returns the start to end substring
String substring (start,end); Returns a substring of start to end-1
6, reverse.
StringBuffer reverse ();string Inversion

StringBuilder string Buffers:
JDK1.5 appears stringbuiler; Constructs a string generator with no characters, with an initial capacity of 16 characters. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread.
Method is the same as StringBuffer;

The difference between StringBuffer and StringBuilder:
StringBuffer thread safety.
StringBuilder thread is not secure.

Single-threaded operation with high StringBuilder efficiency.
Multi-threaded operation, using StringBuffer security.
---------------------------------------------------------
StringBuilder sb = new StringBuilder ("ABCDEFG");
Sb.append ("AK"); Abcdefgak
Sb.insert (1, "et");//AETBCDEFG
Sb.deletecharat (2);//ABDEFG
Sb.delete (2,4);//ABEFG
Sb.setlength (4);//ABCD
Sb.setcharat (0, ' K ');//KBCDEFG
Sb.replace (0,2, "hhhh");//HHHHCDEFG

To use a buffer, create the object first.
StringBuffer sb = new StringBuffer ();
Sb.append. Append ("haha");//method call chain.
String s = "abc" +4+ ' Q ';
s = new StringBuffer (). Append ("abc"). Append (4). Append (' Q '). toString ();
---------------------------------------------------------
Class test{
public static void Main (string[] args) {
String S1 = "Java";
String s2 = "Hello";
Method_1 (S1,S2);
System.out.println (s1+ "....." +s2); Java....hello

StringBuilder S11 = new StringBuilder ("Java");
StringBuilder S22 = new StringBuilder ("Hello");
Method_2 (S11,S22);
System.out.println (s11+ "-----" +s22); Javahello-----Hello
}
public static void Method_1 (String s1,string s2) {
S1.replace (' A ', ' k ');
S1 = s2;
}
public static void Method_2 (StringBuilder s1,stringbuilder s2) {
S1.append (S2);
S1 = s2;
}
}
---------------------------------------------------------
Basic data type Object wrapper class: The basic data type is encapsulated as an object based on object-oriented thinking.
Benefits:
1: Basic data can be manipulated through properties and behaviors in the object.
2: The conversion between the base data type and the string can be implemented.

The class name corresponding to the keyword
BYTE byte
Short short pasershort (numstring);
int Integer static method: parseint (numstring)
Long Long
float float
Double Double
Char Character
Boolean Boolean
Basic data type Object wrapper class: All have XXX parsexxx method
There is only one type without the Parse method: Character;

Java Fundamentals (StringBuffer)----and StringBuilder

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.