Java Foundation Consolidation Series (10): Use and comparison of String, StringBuffer, StringBuilder

Source: Internet
Author: User

The main difference between them:

String is a constant of strings and cannot be changed

StringBuffer is a string variable that can be changed, thread-safe

StringBuilder is a string variable that can be changed, thread is unsafe (JDK1.5 appears later)


First, why is a string constant

public static void Main (string[] args) {String S1 = "Hello"; String s2 = "World"; s1 + = S2; System.out.println (S1);  Hello Worlds1 = s1.substring (6, 11); System.out.println (S1); World}
First, we make s1+=s2, and then the value of S1 becomes: Hello World

It appears that the value of the string object S1 has changed, but in fact S1 is assigned a new reference to the string constant called "Hello World", and the reference to "Hello" has disappeared, when "hello" is already an obsolete object. will be recycled by GC.

Then we used the s1.substring (6,11) function, which returned a new reference, and the reference to "Hello world" disappeared, and S1 was a new reference.

Also, the operation of the string object (including ReplaceAll, replace, substring) returns a new string object.

second, the StringBuilder object detailed

Explanation of the API:

A variable sequence of characters for thread safety. A string- like buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.

String buffers can be safely used with multiple threads. These methods can be synchronized if necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved.


The overhead associated with creating a new string object can be very expensive in situations where you need to perform repeated modifications to the string.

public static void Main (string[] args) {stringbuffer S1 = new StringBuffer ("ABCDF"); S1.append ("Ghi");//output: Abcdfghis1.insert (4, "E"); Output:abcdefghi}

third, the special case of string object splicing operator "+"

At this point, we already know that the string object is an immutable constant, then the "+" splicing operation in string, it must also be to return a new object, it will certainly affect performance.

However, there are special cases:

public static void Main (string[] args) {//(1) String S1 = "Hello"; String s2 = "world!"; String s3 = s1 + s2; System.out.println (S3); Output:hello world!//(2) String s = "Hello" + "world!"; /(3) StringBuffer sb = new StringBuffer ("Hello"); Sb.append ("world!");}
in these three cases, sort by speed: (2) > (3) > (1), which is a feature of the JVM, when the concatenation of the string object is interpreted as a concatenation of the StringBuffer object:

String s = "Hello" + "world!"; is considered as a String s = "Hello world!";


iv. The difference between String, StringBuffer, StringBuilder

string is immutable constant

StringBuffer and StringBuilder are variables that can be changed, in which StringBuffer is thread-safe, guaranteed to be synchronous, StringBuilder is not thread-safe, and does not guarantee synchronization.

The StringBuilder class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer. The two methods are basically the same.




Java Foundation Consolidation Series (10): Use and comparison of String, StringBuffer, StringBuilder

Related Article

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.