Differences between string-stringbuilder

Source: Internet
Author: User

 

1
2 Stringbuffer is thread-safe
3 Stringbuilder does not guarantee thread security (introduced in 1.5), which is generally faster than stringbuffer.
4
5 The length of a string object is fixed and cannot be changed, or a new character is appended to the string object. You may use + To concatenate strings to add new characters or strings, + A new string instance is generated. IfProgramThis type of additional string is frequently required and is not recommended. + To concatenate strings. In object-oriented programming, it is best to reuse the generated objects. The generation of objects requires memory space and time. Continuously generating string instances is inefficient.
6 J2se 5 . 0 provides the java. Lang. stringbuilder class. By default, the object generated by this class has a length of 16 characters. You can also specify the initial length. If the length of the attached characters exceeds the maximum length, the stringbuilder object will automatically increase the length to accommodate the appended characters. If you need to frequently append strings, using stringbuilder will greatly improve the program efficiency. Through the following simple test program, we can know the difference in performance.
7 Ü Example 6. 5 Appendstringtest. Java
8 Public Class Appendstringtest {
9 Public Static Void Main (string [] ARGs) {
10 String text = "" ;
11
12 Long Begintime = System. currenttimemillis ();
13 For ( Int I = 0 ; I < 10000 ; I ++ )
14 Text = Text + I;
15 Long Endtime = System. currenttimemillis ();
16 System. Out. println ( " Execution time: " + (Endtime - Begintime ));
17
18 Stringbuilder = New Stringbuilder ( "" );
19 Begintime = System. currenttimemillis ();
20 For ( Int I = 0 ; I < 10000 ; I ++ )
21 Builder. append (string. valueof (I ));
22 Endtime = System. currenttimemillis ();
23 System. Out. println ( " Execution time: " + (Endtime - Begintime ));
24 }
25 }
26
27 In Example 6.5, use + To concatenate the string, and use system. currenttimemillis () to obtain the system time before and after the for loop execution, so that you can know how long the for loop has been executed. Here are the test data on my computer:
28
29 Execution time: 4641
30 Execution time: 16
31 We can see that there is a large gap in execution time, which indicates the use + The burden of concatenating strings. If you need to append strings frequently, stringbuilder is recommended. In fact, for example 6.5, the execution time of the second for loop can be shorter, because append () can also accept the basic data type, so you do not need to use string specially. the valueof () method retrieves a string from the int. The execution time can be greatly shortened as follows:
32 For ( Int I = 0 ; I < 10000 ; I ++ )
33 Builder. append (I );
34 Use stringbuilder to output the string result, you can use the tostring () method. You can use the length () method to learn the character length of the current object, and capacity () can return the size of the characters that the object Currently can accommodate. In addition, stringbuilder also has the insert () method to insert a character into a specified position. If there is a character after this position, all the characters will be moved back; deletechar () the reserve () method can delete characters at the specified position, while the reserve () method can reverse the string. For detailed usage, You can query the API file description of Java. Lang. stringbuilder.
35 Stringbuilder is j2se 5 Class added only after. 0, in j2se 5 If the version earlier than. 0 has the same requirements, java. Lang. stringbuffer is used. In fact, stringbuilder is designed to have the same operation interface as stringbuffer. Using stringbuilder with a single machine that does not have multiple threads makes it more efficient because stringbuilder does not handle synchronization issues. Stringbuffer will solve the synchronization problem. If stringbuilder is operated under multiple threads, use stringbuffer to manage the synchronization problem on its own.
36

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.