Java Learning Lesson 31st (Common Object API)-StringBuffer class &&stringbuilder class

Source: Internet
Author: User
Tags array to string

StringBuffer class


The composition of a string is achieved through this class.

StringBuffer can be used to delete the contents of a string

StringBuffer is a container

Many methods are the same as String


First, the characteristics

StringBuffer string buffers, containers for storing data

1. Variable-length

2. Can store different data types

3. Eventually to be converted into a string for use

4. Modifying a string

Features of the container:

1. Add

Append (data)

Insert (Index,data);

2. Delete

StringBuffer Delete (int start,int end)//with head, not including tail

deleteCharAt(int index) removes the specified position of this sequence char . //

3. Find

Char charAt (Index)

int IndexOf (String)

int LastindexOf (String)

4. Modifications

StringBuffer Replace (start,end,string)

void Setcharat (Intdex,char)

Curd (Increase and deletion check)

5. Other

SetLength (data)

Reseve to invert a string

public class Main {public static void main (string[] args) {//stringbufferdemo ();//Add//stringbufferdemo_1 (); Stringbufferdemo_2 ();//delete Stringbufferdemo_3 ();//modify}public static void Stringbufferdemo () {StringBuffer SB = new StringBuffer (); StringBuffer S1 = sb.append (4); System.out.println (SB); System.out.println (S1); System.out.println (SB = = S1); S1 = Sb.append (True). Append ("Nihao"); System.out.println (SB); Sb.insert (1, "hehe");//Add "hehe" System.out.println (SB) in the 1 position;} public static void Stringbufferdemo_1 () {StringBuffer SB = new StringBuffer ("Qwer"); Sb.insert (2, 5);//qw5ersystem.out.println (SB);} public static void Stringbufferdemo_2 () {StringBuffer SB = new StringBuffer ("Qwer"); Sb.deletecharat (2);//Delete a sb.delete (1, 3);//Clear Sb.delete (0, Sb.length ()); System.out.println (SB);} public static void Stringbufferdemo_3 () {StringBuffer SB = new StringBuffer ("Qwert"); Sb.replace (0, 3, "aaaaaaa"); System.out.println (SB); Sb.setcharat (2, ' G ');//Note is voidsb.setlength (2); System.out.println ("SB =" +SB);//If the length of the set exceeds the string itself, the partialFill System.out.println with blanks ("LEN =" +sb.length ()); Sb.reverse ();//Invert string System.out.println (SB);}}

Construction method: StringBuffer ()

Constructs a string buffer with no characters, with an initial capacity of 16


Variable-length arrays

The data stored in the array, when not saved, will automatically open a new array, the new book Group is usually about 1 time times the length of the original array, and then copy the original array, and then continue to store new data, pay attention to efficiency issues


Second, StringBuilder class

Actually, it's stringbuffer.

The serious difference is that the birth time is different.

StringBuffer: Guaranteed synchronization, Thread safety (JDK1.0): typically used for multithreading

StringBuilder: no guarantee of Synchronization (JDK1.5): typically used for single-threaded, it appears to improve efficiency

StringBuilder should be used preferentially, because it does not guarantee synchronization, so high efficiency


Class Stringbuffer{object obj = new Object ();p ublic stringbuffer append () {synchronized (obj) {}}public stringbuffer delet E () {synchronized (obj) {}} ...}
One more time to judge the lock, so the efficiency is low

Third, Exercise:

public class Main {public static void main (string[] args) {int[] arr = {1,2,3,4,5,6,7,8,9}; String str = arrytostring (arr); System.out.println ("str:" +STR);} /* Change the int array to string */public static string arrytostring (int[] arr) {//string str = "["; StringBuilder str = new StringBuilder (); Str.append ("["); for (int i = 0; i < arr.length-1; i++) {//str + = Arr[i] + ",";//once the string constant pool does not have "," it will be generated in the string constant pool, so try to use Stringbuilderstr.append (arr[i]+ ",");} str + = arr[arr.length-1]+ "]"; Str.append (arr[arr.length-1]+ "]"); return str.tostring ();}}




Java Learning Lesson 31st (Common Object API)-StringBuffer class &&stringbuilder class

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.