I believe that everyone has a good understanding of the difference between String and StringBuffer, but it is estimated that there are still many comrades who are not clear about the working principles of these two classes. Today I will review this concept for everyone, and by the way J2SE 5.0 brings a new character manipulation class - StringBuilder (don't rush to throw bricks at me, I'm still sober, I'm not talking about C# here, Java also has a 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 situations? I would like to share my views on these categories, and I also hope that everyone can give their opinions. Everyone has made mistakes, and while correcting them, it is a good opportunity to learn.
Briefly speaking, the main performance difference between the String type and the StringBuffer type is actually that String is an immutable object (Why? Ask the designers of Java, why is String not a native type?) Therefore, every time the String type is changed, In fact, 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 String for strings that often change content. , because every time an object is generated, it will have an impact on system performance. Especially when there are too many unreferenced objects in the memory, the JVM's GC will start to work, and the speed will definitely be quite slow. Here is an example that is not very appropriate:
String S1 = "abc";
For(int I = 0; I < 10000; I ++) // For simulates multiple calls of the program
{
S1 + = "def";
S1 = "abc";
}
If this is the case, after the for loop is completed, if the objects in the memory have not been cleared by the GC, there will be more than 20,000 in the memory, an astonishing number, and if this is a system used by many people, then The number is not very large, so everyone must be careful when using it.
If you use the StringBuffer class, the results will be different. Each time the result will be an operation on the StringBuffer object itself, instead of generating a new object and then changing the object reference. So in general we recommend using StringBuffer, especially when string objects change frequently. In some special cases, the string concatenation of String objects is actually interpreted by the JVM as the concatenation of StringBuffer objects, so in these cases the speed of String objects will not be slower than that of StringBuffer objects, and especially the following string objects are generated Among them, String efficiency is much faster than 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 String S1 objects is simply too fast, and at this time StringBuffer does not have an advantage at all in speed. In fact, this is a trick of the JVM. In the eyes of the JVM, this
String S1 = “This is only a” + “simple” + “test”; In fact, it is: String S1 = “This is only a simple test”; So of course it doesn’t take much time. But what everyone should note here is that if your string comes from another String object, the speed will not be that fast, for example:
String S2 = “This is only a”;
String S3 = "simple";
String S4 = "test";
String S1 = S2 +S3 + S4;
At this time, the JVM will behave in the original way, and the generation speed of S1 objects will not be as fast as before. We can do a test to verify it later.
From this we get the first step conclusion: In most cases StringBuffer > String
And how does StringBuilder compare with them? Let me briefly introduce it first. StringBuilder is a newly added class in JDK5.0. The difference between it and StringBuffer is as follows (source: JavaWorld):
Java.lang.StringBuffer Thread-safe mutable character sequence. A string buffer similar to String, but cannot be modified. String buffers can be used safely by multiple threads. These methods can be synchronized when necessary, so that all operations on any particular instance appear to occur in a serial order consistent with the order of method calls made by each thread involved.
Each string buffer has a certain capacity. As long as the length of the character sequence contained by the string buffer does not exceed this capacity, there is no need to allocate a new internal buffer array. This capacity is automatically increased if the internal buffer overflows. Starting from JDK 5.0, an equivalent class for single thread use, StringBuilder, has been added to this class. The StringBuilder class should generally be used in preference to this class because it supports all the same operations but is faster because it does not perform synchronization.
But it is unsafe to use an instance of StringBuilder with multiple threads. If such synchronization is required, it is recommended to use StringBuffer.
With this said, I think everyone can understand the difference between them, so let's make a general derivation below:
In most cases StringBuilder > StringBuffer
Therefore, according to the transfer theorem of this inequality: in most cases StringBuilder > StringBuffer > String (the greater the number of operations, the more stable it is).
Get the system time long start = System.currentTimeMillis(); long end = System.currentTimeMillis(); to know the running millisecond value.
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 palindrome string");
else
JOptionPane.showMessageDialog(null, "Not a palindrome string");
}
}
*/
if(str.equals(str2))
JOptionPane.showMessageDialog(null,"is a palindrome string");
else
JOptionPane.showMessageDialog(null, "Not a palindrome string");
}
}