Java String/StringBuffer/StringBuilder

Source: Internet
Author: User
Tags stringbuffer

Recently, I found that the quality of Java code is not high enough for team members. I am going to write some basic articles for your reference.

I. Definition

String is an unchangeable character sequence.
StringBuffer is a variable character sequence.
StringBuilder is also a variable character sequence.

1. The only difference between StringBuffer and StringBuilder

The StringBuffer object is thread-safe, which means that the StringBuffer object can be modified by multiple parallel threads at the same time because all its methods are declared as "synchronized (synchronous )".
The StringBuilder class is a non-thread-safe class introduced by JDK 1.5, which means that all its methods are non-synchronous.
Therefore, in the application of a single model, we should use StringBuilder so that the object will not be locked and unlocked, and the performance will increase.
In a single-threaded model application, operations are performed sequentially, so the object will not crash.

2. When to select String and StringBuffer

If we do not need to store String modifications in the same memory space, we must use String.
If you need to store string modifications in the same memory space, you must use StringBuffer or StringBuilder.

3. Advantages and disadvantages of String

Advantage: If the String is modified when the String is used, the modified content will be stored in different locations in the memory. In this case, the memory contains both the original string value and the modified string value.
Disadvantage: for each such operation, it consumes more memory because it stores the modified string value in the new memory space. Therefore, it causes performance problems.
Solution: to solve this performance problem, developers should use StringBuilder or StringBuffer to modify the String, and then convert it to a String object to pass the String to the user.

4. Advantages and disadvantages of StringBuffer and StringBuilder

Advantage: StringBuffer and StringBuilder have better performance because they consume less memory and all the string modification operations are stored in the same memory location.
Disadvantage: the original string value before modification is not retained.

2. Create a String or StringBuffer object

You can create a String object in either of the following ways:

1) create an object using string assignment directly

String str = "instance of java for us ";

2) use the String constructor to create an object

String str = new String ("instance of java for us ");

Create a StringBuffer object

Use constructor

StringBuffer str = new StringBuffer ();


Create a StringBuilder object
Use constructor

StringBuilder str = new StringBuilder ();

3. Only the specified operations can be performed in StringBuffer and StringBuilder.

Append, insert, delete, reverse, and other operations on strings
Because the String object is an unchangeable object, these operations cannot be performed in the String object.

IV. String connection

You can use a String object to connect a new String to an existing String in either of the following ways:

1. Use the "+" operator
2. Use the concat () method

There is only one method to connect strings using StringBuffer: The append () method
However, there is only one method to connect strings using StringBuilder: The append () method

Sample code:

Package com. ch;
Public Class Demo {
Public static void main (String args []) {
String str = "Java ";
StringBuffer sb = new StringBuffer ("Java ");
StringBuilder sbr = new StringBuilder ("Java ");

System. out. println (str. concat ("language "));
System. out. println (sb. append ("language "));
System. out. println (sbr. append ("language "));
    }
}

Output:

Java language
Java language
Java language

V. Comparison

The equals () method of an Object is defined as follows: if objects a and B are referenced by the same Object, a. equals (B) returns true; otherwise, false.
The String class overrides the equals () method of the Object. The equals () method of the String Object compares the content. If the content is equal, true is returned.
StringBuffer and StringBuilder do not override the equals () method, which is the same as the definition of the equals () method of the Object.

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.