Differences between string and stringbuffer in Java [reprint]

Source: Internet
Author: User

The difference between string and stringbuffer can be said to be numerous online materials. However, when I saw this article, I felt that the small examples in the article were representative. So I made a summary.

There are three classes in Java to take charge of character operations.

1. character operates on a single character,

2. String operations on a string of characters. Immutable class.

3. stringbuffer also operates on a string of characters, but it can be a variable class.

String:
Yes. The object is not of the original type.
An unchangeable object. Once created, its value cannot be modified.
To modify an existing String object, create a new object and save the new value.
String is a final class, which cannot be inherited.

Stringbuffer:
It is a mutable object. objects are not re-created as strings when modified.
It can only be created by constructors,
Stringbuffer sb = new stringbuffer ();
Note: you cannot assign values to a value using the value assignment symbol.
SB = "Welcome to here! "; // Error
After the object is created, the memory space is allocated in the memory, and a NULL is initially saved to stringbuffer.
You can use its append method to pay a value.
SB. append ("hello ");

Stringbuffer is more efficient than string in string connection operations:

String STR = new string ("Welcome ");
STR + = "here ";
The process is actually by creating a stringbuffer, then calling append (), and finally
Then convert stringbuffer tosting ();
In this case, the string connection operation is more than the stringbuffer operation, of course, the efficiency is greatly reduced.

Since the string object is an immutable object, each operation of sting creates a new object to save the new value.
In this way, the original object will be useless and it will be recycled. This will also affect the performance.

Take a look at the following code:
Repeat 26 English letters for 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. }
  8. Long lend1 = system. currenttimemillis ();
  9. Long time = (lend1-lstart1 );
  10. System. Out. println (time );

Unfortunately, my computer is not a supercomputer, and the result is usually about 46687.
That is, 46 seconds.
Let's take a look at the following code:

  1. String tempstr = "abcdefghijklmnopqrstuvwxyz ";
  2. Int times = 5000;
  3. Long lstart2 = system. currenttimemillis ();
  4. Stringbuffer sb = new stringbuffer ();
  5. For (INT I = 0; I <times; I ++ ){
  6. SB. append (tempstr );
  7. }
  8. Long lend2 = system. currenttimemillis ();
  9. Long time2 = (lend2-lstart2 );
  10. System. Out. println (time2 );

The result is 16. Sometimes it is 0.
Therefore, the conclusion is obvious that the stringbuffer speed is almost ten thousand times faster than that of string. Of course, this data is not very accurate. Because the number of cycles is 100000, the difference is greater. Try it.

According to the above:

STR + = "here ";
In fact, by creating a stringbuffer, let Hou call append (), and finally
Then convert stringbuffer tosting ();

So STR + = "here"; can be equivalent

Stringbuffer sb = new stringbuffer (STR );

SB. append ("here ");

STR = sb. tostring ();

Therefore, the code that uses "+" to connect to a string is basically equivalent to the following code:

  1. String tempstr = "abcdefghijklmnopqrstuvwxyz ";
  2. Int times = 5000;
  3. Long lstart2 = system. currenttimemillis ();
  4. String STR = "";
  5. For (INT I = 0; I <times; I ++ ){
  6. Stringbuffer sb = new stringbuffer (STR );
  7. SB. append (tempstr );
  8. STR = sb. tostring ();
  9. }
  10. Long lend2 = system. currenttimemillis ();
  11. Long time2 = (lend2-lstart2 );
  12. System. Out. println (time2 );

The average execution time is about 46922, that is, 46 seconds.

Conclusion: If you need to frequently modify the connection operations of strings in the program, the performance of stringbuffer is higher.

There are three classes in Java to take charge of character operations.

1. character operates on a single character,

2. String operations on a string of characters. Immutable class.

3. stringbuffer also operates on a string of characters, but it can be a variable class.

String:
Yes. The object is not of the original type.
An unchangeable object. Once created, its value cannot be modified.
To modify an existing String object, create a new object and save the new value.
String is a final class, which cannot be inherited.

Stringbuffer:
It is a mutable object. objects are not re-created as strings when modified.
It can only be created by constructors,
Stringbuffer sb = new stringbuffer ();
Note: you cannot assign values to a value using the value assignment symbol.
SB = "Welcome to here! "; // Error
After the object is created, the memory space is allocated in the memory, and a NULL is initially saved to stringbuffer.
You can use its append method to pay a value.
SB. append ("hello ");

Stringbuffer is more efficient than string in string connection operations:

String STR = new string ("Welcome ");
STR + = "here ";
The process is actually by creating a stringbuffer, then calling append (), and finally
Then convert stringbuffer tosting ();
In this case, the string connection operation is more than the stringbuffer operation, of course, the efficiency is greatly reduced.

Since the string object is an immutable object, each operation of sting creates a new object to save the new value.
In this way, the original object will be useless and it will be recycled. This will also affect the performance.

Take a look at the following code:
Repeat 26 English letters for 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. }
  8. Long lend1 = system. currenttimemillis ();
  9. Long time = (lend1-lstart1 );
  10. System. Out. println (time );

Unfortunately, my computer is not a supercomputer, and the result is usually about 46687.
That is, 46 seconds.
Let's take a look at the following code:

  1. String tempstr = "abcdefghijklmnopqrstuvwxyz ";
  2. Int times = 5000;
  3. Long lstart2 = system. currenttimemillis ();
  4. Stringbuffer sb = new stringbuffer ();
  5. For (INT I = 0; I <times; I ++ ){
  6. SB. append (tempstr );
  7. }
  8. Long lend2 = system. currenttimemillis ();
  9. Long time2 = (lend2-lstart2 );
  10. System. Out. println (time2 );

The result is 16. Sometimes it is 0.
Therefore, the conclusion is obvious that the stringbuffer speed is almost ten thousand times faster than that of string. Of course, this data is not very accurate. Because the number of cycles is 100000, the difference is greater. Try it.

According to the above:

STR + = "here ";
In fact, by creating a stringbuffer, let Hou call append (), and finally
Then convert stringbuffer tosting ();

So STR + = "here"; can be equivalent

Stringbuffer sb = new stringbuffer (STR );

SB. append ("here ");

STR = sb. tostring ();

Therefore, the code that uses "+" to connect to a string is basically equivalent to the following code:

  1. String tempstr = "abcdefghijklmnopqrstuvwxyz ";
  2. Int times = 5000;
  3. Long lstart2 = system. currenttimemillis ();
  4. String STR = "";
  5. For (INT I = 0; I <times; I ++ ){
  6. Stringbuffer sb = new stringbuffer (STR );
  7. SB. append (tempstr );
  8. STR = sb. tostring ();
  9. }
  10. Long lend2 = system. currenttimemillis ();
  11. Long time2 = (lend2-lstart2 );
  12. System. Out. println (time2 );

The average execution time is about 46922, that is, 46 seconds.

Conclusion: If you need to frequently modify the connection operations of strings in the program, the performance of stringbuffer is higher.

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.