Reference link: www.jb51.net/article/107…

A list,

In the Java language, there are eight basic data types: Byte, short, int, long, float, double, Boolean, and char, where char is used to represent a single character, such as a, B, C, a, B, C, &, or special characters. In real programming, individual characters are not used as often as we think. Instead, “strings” of multiple characters are more common, but there is no such thing as a string in the basic data type. To solve this problem, the Java language provides us with a class String modified with the final keyword, which represents a String type, and since it is modified with final, this means that we can only create strings and call methods from this class, but not inherit from it.

Although the Java language provides us with a String class that makes it easy to use strings, let’s say so. But in the process of development, we find that the pure String type is not enough to meet our needs. Thus, StringBuffer and StringBuilder are derived from String.

Second, the String

Now we have an overview of the String type. It is said to be a datatype, but it is not a basic datatype, but a final modified, non-inheritable class in the java.lang package. As for how to use String, there are two methods, one is directly assigned, the other is created with new, as shown in the following example:

String str1 = String str1 = String str1 = String str1 = String str1 = String str1 ="Vitamin C fructose"; Str2 = new String()"Vitamin C fructose");Copy the code

In common String operations, particularly common determine whether two strings are equal, and there are two kinds of commonly used discriminant method, which USES the String class provides methods and equals the = = operator, the following is the use of high frequency of the String class API method:

Boolean endsWith(String suffix) //* Returns if the String ends in suffixtrueOtherwise returnfalse*/ Boolean equals(Object other) /* Returns if the string equals othertrueOtherwise returnfalse*/ Boolean equalsIgnoreCase(String other) /* Returns if the String is equal to other (case ignored)trueOtherwise returnfalse*/ int length() /* Returns the length of the String */ String replace(CharSequence oldString, CharSequence newString) / This string replaces all oldString strings in the original string with newString strings, */ Boolean startsWith(String prefix) /* Returns if the String begins with prefixtrueOtherwise returnfalseString subString (int beginIndex, int endIndex) /* Returns a new String, This String contains all the code units in the original String from beginIndex to the end or endIndex-1 position */ String toLowerCase() /* Returns a new String, */ String toUpperCase() /* Returns a new String, This String replaces all lowercase characters in the original String with uppercase characters */ String trim() /* Returns a new String that removes Spaces from the meta String header and tail */Copy the code

Third, StringBuffer

When we learn about String, we find that it has some drawbacks. For example, when we create a String object, it is difficult to add, delete, or modify it. To solve this problem, the Java language introduces the StringBuffer class. StringBuffer is similar to String, except that it doesn’t generate new objects when it does String processing. So StringBuffer is superior to String in memory usage.

There are many methods in The StringBuffer class that are exactly the same as those in the String class. One very significant difference, however, is that every time a StringBuffer object is modified, it is modifying the object itself, which is the biggest difference between it and the String class.

In addition, StringBuffers are thread-safe and can be used with multiple threads.

When a thread accesses a resource, it locks it, and other threads cannot use it until the thread releases the resource. This is to prevent abnormal changes and use of data, which are usually global variables in the program

And the initialization of a StringBuffer is not the same as the initialization of a String. Normally, we use the constructor:

// Declare an empty StringBuffer object StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer("Vitamin C fructose"); // The following assignment statement is wrong because StringBuffer and String are different types StringBuffer sb ="Vitamin C fructose"; // The following assignment statement is also wrong because StringBuffer does not inherit from String"Vitamin C fructose"; StringBuffer sb = new StringBuffer("Vitamin C fructose");
String str = sb.toString();
Copy the code

Here are the common API methods for StringBuffer

StringBuffer() /* Constructs an empty String builder */ int Length () /* Returns the number of code units (characters) in the builder or buffer */ StringBuffer append(String STR) /* Append a string and return this */ StringBuffer append(Char c) /* Append a character and return this */ voidsetCharAt(int i, Char C) /* Sets the i-th code unit to C */ StringBuffer Reverse () /* Reverses the contents of the builder sequentially C */ String toString() /* Returns a String identical to the contents of the builder or buffer */Copy the code

Fourth, the StringBuilder

After JDK 5.0, the Java language introduced the StringBuilder class, formerly known as StringBuffer, which is slightly less efficient but allows you to add and remove characters in a multithreaded manner. If all strings are edited in a single thread (which is usually the case), then StringBuilder should be used instead; the API for the two classes is exactly the same.

Five, the summary

Now that we’ve looked at String, StringBuffer, and StringBuilder in detail, we know that all three of them are classes for manipulating strings. Let’s summarize some of the differences

  • For efficiency, StringBuilder > StringBuffer > String;

  • For thread-safe purposes, StringBuffers are thread-safe and can be used with multiple threads; StringBuilder is non-thread-safe for a single thread;

  • Both StringBuffer and StringBuilder are superior to String for frequent String operations.