The difference analysis between StringBuffer and string in Java _java

Source: Internet
Author: User

See this is a good explanation, so turn around

There are 3 classes in Java that are responsible for the operation of characters.

1.Character is a single character operation,

2.String operation on a string of characters, immutable class.

3.StringBuffer also operates on a string of characters and is a variable class.

String:
Is that the object is not the original type.
is an immutable object, and once created, it cannot modify its value.
The modification of a string object that already exists is to re-create a new object and then save the new value in.
String is the final class, which cannot be inherited.

StringBuffer:
is a mutable object that, when modified, does not re-establish objects like string
It can only be built through constructors,
StringBuffer sb = new StringBuffer ();
Note: The value cannot be paid by the assignment symbol.
SB = "Welcome to here!"; /error
After the object is established, the memory space is allocated in memory and a null is initially saved. You can pass its append () method when assigning values to StringBuffer.
Sb.append ("Hello");

StringBuffer is more efficient in string concatenation operations than string:

String str = new String ("Welcome to");
str = "Here";
The processing step is actually by creating a stringbuffer and then calling append (), and finally
Then the StringBuffer tosting ();
In this way the string concatenation operation is more than StringBuffer, of course, the efficiency of the discount.

And because a string object is an immutable object, each operation sting a new object to hold the new value.
So the original object is useless, it will be garbage collection. This is also to affect performance.

Look at the following code:
Repeat the 26 English letters 5,000 times,

Copy Code code 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, the results are not necessarily the same as the general 46687 or so.
Which is 46 seconds.
Let's take another look at the following code
Copy Code code 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, sometimes 0.
So the conclusion is obvious that StringBuffer is almost as fast as a string of tens of thousands of times. Of course, this data is not very accurate. Because the number of cycles in 100,000 times, the difference is greater. Don't believe you try

If you still can't understand:

1 The difference between the combined + method of string and the Stringbuff Append method:

When a string operation is performed, the string's + operator first converts the current string object to the Stringbuff type, calls its Append method, and finally converts the generated Stringbuff object to a string by its ToString method. So its efficiency is low.

However, for readability, the concatenation operator of string is higher.

2) Stringbuff is thread-safe

String is not safe for threads

3 string is a string object that cannot be modified, and Stringbuff can be modified.

Copy Code code 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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.