Home >Java >javaTutorial >The difference between String and StringBuffer in JAVA
The difference between String and StringBuffer can be said to be countless on the Internet, but when I saw this article, I felt that the small examples in it were very representative, so I turned to it and made a summary myself.
There are 3 classes in java that are responsible for character operations.
1.Character operates on a single character, and
2.String operates on a string of characters. Immutable classes.
3.StringBuffer also operates on a string of characters, but it is a variable class.
String:
is an object that is not a primitive type.
is an immutable object. Once it is created, its value cannot be modified.
Any modification to an existing String object is to create a new object and then add the new one. The value is saved in.
String is a final class, that is, it cannot be inherited.
StringBuffer:
is a mutable object. When it is modified, the object will not be re-established like String.
It can only be created through the constructor ,
StringBuffer sb = new StringBuffer();
note: It cannot be paid through the value symbol.
sb = "welcome to here!";//error
After the object is created, memory will be allocated in the memory space, and initially saves a null. When paying value to StringBuffer
, you can use its append method.
sb.append("hello");
StringBuffer is more efficient than String in string connection operations:
String str = new String("welcome to ");
str += "here"; The processing steps of
are actually to create a StringBuffer, let you call append(), and finally
then convert StringBuffer toSting();
like this In this case, String's connection operation requires some additional operations than StringBuffer, but of course the efficiency will be compromised.
And since the String object is an immutable object, a new object will be re-established every time Sting is operated to save the new value.
This way The original object is useless and will be garbage collected. This will also affect performance.
Look at the following code:
Added 26 English letters 5000 times,
1. String tempstr = "abcdefghijklmnopqrstuvwxyz" ;
2. int times = 5000;
3. long lstart1 = System.currentTimeMillis();
4. String str = "";
5. for (int i = 0; i < times; i++) {
6 . str += tempstr;
7. long lend1 = System.currentTimeMillis(); The computer is not a supercomputer, and the result obtained may not be the same every time, usually around 46687.
That’s 46 seconds.
Let’s take a look at the following code again
1. String tempstr = "abcdefghijklmnopqrstuvwxyz";
2. int times = 5000;
3. long lstart2 = System.currentTimeMillis();
5. for (int i = 0; i < times; i++) {
6. sb.append(tempstr);
7. long lend2 = System.currentTimeMillis();
9. long time2 = ( lend2 - lstart2);
10. System.out.println(time2);
The result is 16 and sometimes 0
So the conclusion is obvious, StringBuffer is almost tens of thousands times faster than String. Of course this data is not very accurate. Because when the number of cycles is 100,000, the difference is even greater. If you don’t believe me, try it.
According to the above:
The processing step is actually to create a StringBuffer, let you call append(), and finally
then StringBuffer toSting();so str += "here"; can be equivalent to
StringBuffer sb = new StringBuffer(str);
1. String tempstr = "abcdefghijklmnopqrstuvwxyz";
2. int times = 5000;
3. long lstart2 = System.currentTimeMillis(); int i = 0; i < times; i++) {
6. StringBuffer sb = new StringBuffer(str);
7. sb.append(tempstr);
8. str = sb. long lend2 = System.currentTimeMillis();
11. long time2 = (lend2 - lstart2);
12. System.out.println(time2);
The average execution time is about 46922, which is 46 seconds.
Summary: If you need to frequently modify and connect strings in your program, the performance will be higher using StringBuffer