|
Foreword
Recently I found that the quality of the team in terms of Java code is not high enough, ready to write some basic articles, for your reference.
First, the definition of
String is immutable character sequence.
StringBuffer is mutable sequence of characters.
StringBuilder is mutable sequence of characters.
The only difference between 1, StringBuffer and StringBuilder of
StringBuffer objects are thread-safe, which means that StringBuffer object can be simultaneously modify multiple parallel threads, because all of its methods are declared "synchronized (synchronous)."
StringBuilder class is not thread-safe class introduced in JDK 1.5 version, which means that all of its methods are asynchronous method.
Thus, in the application of a single model, we should use StringBuilder, so the object will not be locked and unlocked, so performance will increase.
In a single-threaded model, and performs operations in sequence, the object will not collapse.
2. When selected String and StringBuffer
If we do not need to modify the string stored in the same memory space, then we must use the String.
And if you need to modify string stored in the same memory space, then we must use StringBuffer or StringBuilder.
3, String advantages and disadvantages
Advantages: different locations using String, if the string is modified, the modification is saved in memory, in this case, the memory will have a string value while the value of the original string and the modified.
Cons: For each one of these operations, it will consume more memory, since it is the new string value stored in the memory space after modification. Therefore, it can cause performance problems.
Solution: To solve this performance problem, developers should use StringBuilder or StringBuffer to achieve modification of the string, then converted to a String object to pass a string to the user.
4, StringBuffer and StringBuilder advantages and disadvantages
Advantages: StringBuffer and StringBuilder have better performance because they consume less memory, all modification operations on strings are stored in the same memory location.
Disadvantages: original string value before the change will not be retained.
Second, create a String or StringBuffer object
Create a String object in two ways
1) directly create objects using string assignment
String str = "instance of java for us";
2) create an object using the String constructor
String str = new String ( "instance of java for us");
Creating StringBuffer object
Use constructor
StringBuffer str = new StringBuffer ();
Create StringBuilder object
Use constructor
StringBuilder str = new StringBuilder ();
Third, only the specified action in StringBuffer and StringBuilder
Appending the string may be performed, insert, delete, and other operations reverse
Because String objects are immutable objects, so these operations can not be performed in a String object.
Fourth, the connection string
String object to use of the new string to connect to an existing string, there are two ways:
1, using the "+" operator
2, using the concat () method
The use of StringBuffer connection string, only one way: using the append () method
Use StringBuilder connection string, only one way: using 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
Fifth, compare
Object definition of equals () method is: a and b if the object is a reference to the same object, then a.equals (b) returns true, otherwise returns false.
The String class overrides Object's equals () method, equals String object () method compares the contents, the contents are equal returns true.
StringBuffer and StringBuilder no rewrite equals () method, with the same definition of Object equals () method. |
|
|
|