Java common classes: StringBuffer and StringBuilder

Source: Internet
Author: User

Java common classes: StringBuffer and StringBuilder
A StringBuilder class (1) understand the StringBuilder class

In Java, in addition to using the String class to store strings, you can also use the StringBuilder class or StringBuffer class to store strings.

StringBuilder is more efficient than String when you need to modify strings frequently.

The String class is non-mutable. For example:

Running result:

We can see from the running results that an additional object will be created during the program running to save "helloworld ". When the string is frequently operated

Generate many temporary variables. Use StringBuilder or StringBuffer to avoid this problem. As for StringBuilder and StringBuffer, they

Basically, the difference is that StringBuffer is thread-safe, while StringBuilder does not implement the thread security function, so the performance is slightly higher. Therefore

In general, if you want to create a variable String object, use the StringBuilder class first.

Define and create the StringBuilder class instance code:

Running result: imooc

(2) Common StringBuilder class methods

The StringBuilder class provides many methods to operate strings:

For example, in the following sample code, a StringBuilder object is created to store strings and append and insert strings. These operations

Modify the value of the str object without creating a new object. This is the biggest difference between StringBuilder and String.

Instance code:

Running result:

After learning about the StringBuilder class, let's try to complete the following functions.

Function: converts a string consisting of English letters into a specified format. Each of the three letters on the right are separated by commas.

Instance code:
Public class Test {public static void main (String [] args) {// create an empty StringBuilder object StringBuilder str = new StringBuilder (); // append the String str. append ("jaewkjldfxmopzdm"); // insert a comma for (int I = str. length ()-3; I> 0; I = I-3) {str. insert (I, ",") ;}// convert the StringBuilder object to a String object and Output System. out. print (str. toString ());}}

Running result:

Ii. StringBuffer class (1) Understanding the StringBuffer class

Java. lang. StringBuffer represents a variable character sequence. StringBuffer is similar to String, but StringBuffer can modify its String.

Change.

(2) Common StringBuffer class construction methods:

StringBuffer ()

Create a "null" StringBuffer object that does not contain character sequences.

StringBuffer (String str)

Create a StringBuffer object that contains the same character sequence 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);     }}

 

Running result:

(3) Common StringBuffer Methods

1) The overload method public StringBuffer append (...) can add a character sequence to the StringBuffer object and return the added StringBuffer pair.

Example:

2) The overload method public StringBuffer insert (...) can insert character sequences for the StringBuffer object at the specified position. Return the modified

StringBuffer object reference, for example:

3) The public StringBuffer delete (int start, int end) method can delete a character sequence from start to end-1. After the modification is returned

The StringBuffer object reference.

4) methods similar to the meaning of the String class:

5) The public StringBuffer reverse () method 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 ('/'). 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 the running result:

Three StringBuffer classes and StringBuilder classes

When modifying a string, use the StringBuffer and StringBuilder classes. Different from the String class, StringBuffer and

The StringBuilder class objects can be modified multiple times without generating new unused objects. The StringBuilder class is proposed in Java 5 and

The biggest difference between StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously ). Compared

StringBuffer has a speed advantage, so the StringBuilder class is recommended in most cases. However, when the application requires thread security

Use the StringBuffer class.

The StringBuffer class supports the following methods:

 

Similar to the method of the String 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.