String & amp; StringBuffer & amp; StringBuilder, javaString in java

Source: Internet
Author: User

Java String & StringBuffer & StringBuilder, javaString
1.

String, String constant

StringBuffer, string variable (thread safety)

StringBuilder, string variable (non-thread safety)

2. String and StringBuffer

The main difference between String and StringBuffe is that String is an immutable object. Every time you change the String, A New String object is generated, therefore, do not use the String type for strings that often change the content, because every time a new object is formed, the system performance will be affected, especially when there are more non-referenced objects in the memory, the GC of JVM will start to work, and the speed will be quite slow.

Every StringBuffer operation is performed for itself, instead of forming a new object and changing the object reference. Therefore, we recommend using StringBuffer in general, this is especially true when character string objects change frequently. in some special cases, String concatenation of String objects is actually interpreted as concatenation of StringBuffer objects. Therefore, the speed of String objects is not slower than that of StringBuffer in these cases, in particular, the efficiency of String is faster than that of StringBuffer during the formation of the following String objects:

String s1 = 'this is only a' + 'simple' + 'test ';

StringBuffer buffer = new StringBuffer ('this is only A'). append ('simple'). append ('test ');

In the eyes of JVM, String s1 = 'this is only a' + 'simple' + 'test' is actually String s1 = 'this is only a simple test ';

But if so:
String s2 = 'this is only ';

String s3 = 'simple ';

String s4 = 'test ';

String s1 = s2 + s3 + s4;

At this time, the JVM will follow the original method,

In most cases: StringBuffer> String

3. StringBuffer

Java. lang. variable Character Sequence of StringBuffer thread security, a String buffer similar to String, but cannot be modified. although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling a method.

The string buffer can be safely used for multiple threads. these methods can be synchronized when necessary. Therefore, all operations on any specific instance may occur in a serial order, which is consistent with the execution sequence of the thread that calls the method.

During major operations on StringBuffer, append and insert operations can reload these methods to accept any data type. Each method can effectively convert the given data to a string, append the string or insert it into the character buffer.

4. StringBuilder

Java. lang. the StringBuilder thread is a variable string sequence that is not secure and is newly added in 5.0. this class provides a StringBuffer-compatible API, but does not guarantee synchronization. this class is designed as a simple replacement of StringBuffer. This class is preferred when the string buffer is used by a single thread. In most cases, StringBuilder is faster than StringBuffer, the two methods are basically the same.

5 \\

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.