First, Lang order
This blog post originates from reprint, the original post address is: http://blog.chinaunix.net/uid-301743-id-5032902.html
Second, the content
Objective
Recently found that the team members in the Java code quality is not high enough, ready to write some basic articles for your reference.
First, the definition
String is a sequence of immutable characters.
StringBuffer is a variable sequence of characters.
StringBuilder is also a variable sequence of characters.
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 of its methods are declared as "synchronized (synchronous)".
The StringBuilder class is a non-thread-safe class introduced in JDK version 1.5, which means that all of its methods are non-synchronous.
Therefore, in a single model application, we should use StringBuilder, so that the object will not have locking and unlocking, so that performance will increase.
In single-threaded model applications, operations are performed sequentially, so objects do not crash.
2. When to select string and StringBuffer
If we do not need to store the modification of the string in the same memory space, we must use string.
If you need to store the modification of the string in the same memory space, then we must use StringBuffer or StringBuilder.
3. Advantages and disadvantages of string
Advantage: When using string, if the string is modified, the modified content is saved in different places in memory, so that the original string value and the modified string value are in memory.
Cons: For each of these operations, it consumes more memory because it stores the modified string values in the new memory space. Therefore, it can cause performance problems.
Solution: To solve this performance problem, developers should use StringBuilder or StringBuffer to implement string modifications and then convert them to string objects to pass the string to the user.
4. Advantages and disadvantages of StringBuffer and StringBuilder
Pros: StringBuffer and StringBuilder have better performance because they consume less memory and all modifications to strings are stored in the same memory location.
Disadvantage: The original string value before modification is not preserved.
Second, create a string or StringBuffer object
There are two ways to create a string object
1) Create an object directly using a string assignment
2) Creating an object using the string constructor
Creation of StringBuffer objects
Using constructors
Creation of StringBuilder objects
Using constructors
Third, the StringBuffer and StringBuilder can only perform the specified operation
You can perform append, insert, delete, reverse, and so on strings
Because the string object is an immutable object, these operations cannot be performed in a string object.
Iv. Connection of strings
There are two ways to connect a new string to an existing string using a String object:
1, using the "+" operator 2, using the Concat () method
Instead of using the StringBuffer connection string, there is only one way: Use the Append () method
Instead of using the StringBuilder connection string, there is only one way: Use the Append () method
Example 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:
V. Comparison
The object's definition of the Equals () method is that if objects A and B are references to the same object, then A.equals (b) returns true, otherwise false.
While the string class overrides the Equals () method of object, the Equals () method of the string object compares the contents and returns true if the content is equal.
StringBuffer and StringBuilder do not override the Equals () method, which is the same as the definition of the Equals () method of object.
----------------------------------------Youth----------------------------------------
Java's string and StringBuffer and StringBuilder explanations