Seeing this explanation is good, so I'll turn it around
There are 3 classes in Java that are responsible for character operations.
1. Character is a single character operation.
2.String operates on a string of characters, immutable class.
3.StringBuffer is also an operation on a string of characters and is a mutable class.
String:
It is an object, not a primitive type.
It is an immutable object and once it is created, its value cannot be modified.
Modification of existing String objects is to recreate a new object and save the new value in it.
String is a final class, that is, it cannot be inherited.
StringBuffer:
It is a mutable object. When modifying it, it will not recreate the object like String.
It can only be created by constructors,
StringBuffer sb = new StringBuffer();
Note: It cannot be paid by assignment symbols.
sb = "welcome to here!";//error
After the object is created, memory space will be allocated in memory and a null will be initially saved. When assigning values to StringBuffer, it can be used to use its append() method.
sb.append("hello");
The efficiency of StringBuffer is higher than that of String in string concatenation:
String str = new String("welcome to ");
str += "here";
The processing step is actually by creating a StringBuffer, then calling append(), and finally
Then add StringBuffer toSting();
In this way, the String connection operation will have some additional operations than StringBuffer, and of course the efficiency will be reduced.
And since the String object is an immutable object, each operation of Sting will recreate a new object to save the new value.
In this way, the original object will be useless and will be garbage collected. This will also affect performance.
Take a look at the following code:
Repeat 26 English letters 5,000 times.
The code copy is as follows:
String tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 5000;
long lstart1 = System.currentTimeMillis();
String str = "";
for (int i = 0; i < times; i++) {
str += tempstr;
}
long lend1 = System.currentTimeMillis();
long time = (lend1 - lstart1);
System.out.println(time);
Unfortunately, my computer is not a supercomputer, and the results I get are not necessarily the same each time, usually around 46687.
That is 46 seconds.
Let's take a look at the following code
The code copy is as follows:
String tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 5000;
long lstart2 = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < times; i++) {
sb.append(tempstr);
}
long lend2 = System.currentTimeMillis();
long time2 = (lend2 - lstart2);
System.out.println(time2);
The result is 16 and sometimes it is 0
So the conclusion is obvious, the speed of StringBuffer is almost tens of thousands of times that of String. Of course, this data is not very accurate. Because the number of cycles is 100,000 times, the difference is even greater. If you don't believe me, try
If you still can't understand:
1) The difference between the joint + method of String and the append method of StringBuff:
When the String + operator performs string operation, it first converts the current string object to StringBuff type, calls its append method, and finally converts the generated StringBuff object to a String type string through its toString method, so it Inefficient.
However, in terms of readability, the connection operator of String is still high.
2) StringBuff is thread-safe
String is non-safe for threads
3) String is a string object that cannot be modified, while StringBuff is modified.
The code copy is as follows:
public static boolean fileCopy(String srcStr, String destStr) {
File srcFile = null;
File destFile = null;
Reader reader = null;
Writer writer = null;
boolean flag = false;
try {
srcFile = new File(srcStr);
if (!srcFile.exists()) {
System.out.println("source file does not exist");
System.exit(0);
} else {
reader = new FileReader(srcFile);
}
destFile = new File(destStr);
writer = new FileWriter(destFile);
char[] buff = new char[1024];
int len;
String str = "";
StringBuffer sbuff = new StringBuffer();
while ((len = reader.read(buff)) != -1) {
// str += new String(buff, 0, len);
sbuff.append(new String(buff,0,len));
}
// writer.write(str.toCharArray());
writer.write(sbuff.toString().toCharArray());
flag = true;
writer.flush();
reader.close();
writer.close();
} catch (IOException e) {
System.out.println("File Copy Exception:= " + e.getMessage());
}
return flag;
}