Javase Introductory Learning 29:java class StringBuffer class and StringBuilder class

Source: Internet
Author: User

one StringBuilder class (1) Recognize StringBuilder class

In Java, in addition to using the string class to store strings, you can also use the StringBuilder class or the StringBuffer class to store strings, where you need to

It is more efficient to use StringBuilder to modify the string frequently than a string.

The String class has a immutability. Such as:


Operation Result:


From the running results we can see that the program runs with an extra object, saving "HelloWorld". When the string is frequently manipulated, the amount

generates many temporary variables outside of the Use StringBuilder or StringBuffer to avoid this problem. As for StringBuilder and StringBuffer, they

basically similar, different, StringBuffer is thread-safe, and StringBuilder does not implement thread-safe functionality, so performance is slightly higher. as a result of this

case, if you need to create a variable-content string object, you should prioritize the use of the StringBuilder class .

Define and create an instance code for the StringBuilder class:


Running Result: IMOOC

(2) Common methods of StringBuilder class

The StringBuilder class provides many ways to manipulate strings:


For example, in the following example code, a StringBuilder object was created to store the string and append and insert it. These exercises

To modify the value of the Str object without creating a new object, which is the biggest difference between StringBuilder and string.

Instance code:


Operation Result:


Having learned about the StringBuilder class, let's try to accomplish the following functions.

Function: Converts a string consisting of an English letter into a specified format---from the right to a comma-delimited form of three letters.

Instance Code:
public class Test {public    static void Main (string[] args) {//Create an empty StringBuilder object StringBuilder str=new StringBuilder ();//Append String Str.append ("JAEWKJLDFXMOPZDM");        Insert comma for (int i=str.length () -3;i>0;i=i-3) {        Str.insert (i, ",") from the forward every three bits;}             Converts the StringBuilder object to a string object and outputs System.out.print (str.tostring ());}    }

Operation Result:


two StringBuffer class (1) Recognize StringBuffer class

Java.lang.StringBuffer represents a variable sequence of characters. StringBuffer is similar to string, but StringBuffer can change its string to

Change.

(2) Common construction methods of the StringBuffer class:

StringBuffer ()

Creates an "empty" StringBuffer object that does not contain a sequence of characters.

StringBuffer (String str)

Creates a StringBuffer object that contains the same sequence of characters as the String Object str.

Instance code:

public class test{public    static void Main (String args[]) {StringBuffer sbuffer = new StringBuffer ("Test");        Sbuffer.append ("String Buffer");        System.out.println (Sbuffer);     }}

Operation Result:


(3) Common methods of StringBuffer class

1) Overloaded method public StringBuffer append (...) You can add a sequence of characters for the StringBuffer object, returning the added StringBuffer to the

Like references, for example:


2) Overloaded method public StringBuffer insert (...) You can insert a sequence of characters at a specified location for the StringBuffer object. Returns the modified

StringBuffer object references, for example:


3) method public StringBuffer Delete (int start,int end) can delete a sequence of characters from start to End-1, returning the modified

The StringBuffer object reference.

4) A method similar to the meaning of the string class:


5) method Public StringBuffer reverse () is used to reverse the character sequence and return the modified StringBuffer object reference.

Instance code:

public class test{public    static void Main (string[] args) {String s = "Mircosoft"; char[] A = {' A ', ' B ', ' C '}; StringBuffer sb1 = new StringBuffer (s), sb1.append ('/'). Append ("IBM"). Append ("Sun"). System.out.println (SB1); StringBuffer SB2 = new StringBuffer ("number"), for (int i=0;i<=9;i++) {    sb2.append (i);} System.out.println (SB2); Sb2.delete (8,sb2.length ()). Insert (0,A); System.out.println (SB2); System.out.println (Sb2.reverse ());}    }         

Compile run Result:


three StringBuffer class and StringBuilder class

When you modify a string, you need to use the StringBuffer and StringBuilder classes. Unlike the string class, StringBuffer and

Objects of the StringBuilder class can be modified more than once, and no new unused objects are generated. The StringBuilder class was presented in Java 5, and it

The biggest difference between stringbuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously). Since the StringBuilder phase is more

StringBuffer has a speed advantage, so it is recommended to use the StringBuilder class in most cases. However, in cases where the application requires thread safety, you must

Use the StringBuffer class.

The main methods supported by the StringBuffer class are:

similar to the method of the string class:


Javase Introductory Learning 29:java class StringBuffer class and StringBuilder class

Related Article

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.