I believe that you have already understood the differences between String and StringBuffer, but it is estimated that many comrades may not be clear about the working principles of these two classes, today, I will review this concept here. By the way, I will show you StringBuilder, a new character operation class introduced in J2SE 5.0, I am still awake. I am not talking about C # Here, and Java also has the StringBuilder class ). So what are the differences between this StringBuilder and StringBuffer and the String class we first met? Which one should we use in different scenarios? I want to share my own views on these categories. I also hope that you can put forward your comments. If everyone has a mistake, it is a good opportunity to learn at the same time.
In short, the main performance difference between the String type and the StringBuffer type is that the String type is an immutable object (why? Ask the Java designer why is String not of the native type ?) Therefore, every time the String type is changed, it is equivalent to generating a new String object, and then pointing the pointer to the New String object, therefore, it is best not to use a String that often changes the content, because every time an object is generated, it will affect the system performance, especially when there are more referenced objects in the memory, the GC of JVM will start to work, and the speed will be quite slow. Here is an incorrect example:
String S1 = "abc ";
For (int I = 0; I <10000; I ++) // multiple calls of the For simulation program
{
S1 + = "def ";
S1 = "abc ";
}
If so, after this for loop is complete, if the objects in the memory are not cleared by GC, there will be more than 20 thousand objects in the memory, an astonishing number, if this is a system that many people use, this number is not much, so you must be careful when using it.
If the StringBuffer class is used, the results will be different. Each result will operate on the StringBuffer object itself, instead of generating a new object, and then change the object reference. Therefore, we generally recommend using StringBuffer, especially when string objects change frequently. In some special cases, the String concatenation of the String object is actually interpreted by JVM as the concatenation of the StringBuffer object. Therefore, the speed of the String object in these cases is not slower than that of the StringBuffer object, in particular, in the generation of the following String objects, the String efficiency is much faster than that of StringBuffer:
String S1 = "This is only a" + "simple" + "test ";
StringBuffer Sb = new StringBuilder ("This is only a"). append ("simple"). append ("test ");
You will be surprised to find that the speed of generating the String S1 object is too fast, and the StringBuffer speed is not dominant at all. In fact, this is a JVM trick.
String S1 = "This is only a" + "simple" + "test"; in fact, it is: String S1 = "This is only a simple test "; so of course it does not take too much time. However, you should note that if your String is from another String object, the speed will not be so fast. For example:
String S2 = "This is only ";
String S3 = "simple ";
String S4 = "test ";
String S1 = S2 + S3 + S4;
At this time, the JVM will follow the original method in a regular manner, and the generation speed of the S1 object is not as fast as it was just now. In a moment, we can come to a test for verification.
From this, we get the first conclusion: In most cases, StringBuffer> String
What if StringBuilder compares with them? First, let's briefly introduce StringBuilder, a newly added class in JDK. For the difference between StringBuffer and StringBuffer, see the following introduction (Source: JavaWorld ):
Java. lang. StringBuffer thread-safe variable character sequence. It is similar to the String buffer, but cannot be modified. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5.0, an equivalent class used by a single thread is added to this class, that is, StringBuilder. Compared with this class, the StringBuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster.
However, it is not safe to apply StringBuilder instances to multiple threads. If such synchronization is required, StringBuffer is recommended.
In this case, we can all understand the differences between them, so let's make a general deduction:
In most cases, StringBuilder> StringBuffer
Therefore, according to the transfer theorem of this inequality: In most cases, StringBuilder> StringBuffer> String (the larger the number of operations, the more stable ).
Obtain the System time long start = System. currentTimeMillis (); long end = System. currentTimeMillis (); then you can know the running millisecond value.
Copy codeThe Code is as follows: import javax. swing. JOptionPane;
Public class T1 {
Public static void main (String args []) {
String str;
String str2;
Int I;
StringBuffer sb = new StringBuffer ();
Str = JOptionPane. showInputDialog (null, "enter a string ");
For (I = 0; I <str. length ()/2; I ++)
If (str. charAt (I )! = Str. charAt (str. length ()-i-1 ))
Break;
If (I> = str. length ()/2)
JOptionPane. showMessageDialog (null, "is a return string ");
Else
JOptionPane. showMessageDialog (null, "not a return string ");
}
}
Ignore non-letter or non-digit characters to determine the return stringCopy codeThe Code is as follows: import javax. swing. JOptionPane;
Public class T2 {
Public static void main (String args []) {
String str;
String str2;
Int I;
StringBuffer sb = new StringBuffer ();
Str = JOptionPane. showInputDialog (null, "enter a string ");
For (I = 0; I <str. length (); I ++)
{
If (Character. isLetterOrDigit (str. charAt (I )))
Sb. append (str. charAt (I ));
}
Str = sb. toString ();
Str2 = sb. reverse (). toString ();
/*
For (I = 0; I <str. length ()/2; I ++)
If (str. charAt (I )! = Str. charAt (str. length ()-i-1 ))
Break;
*/
If (str. equals (str2 ))
JOptionPane. showMessageDialog (null, "is a return string ");
Else
JOptionPane. showMessageDialog (null, "not a return string ");
}
}