String, StringBuffer, and StringBuilder comparisons in Java

Source: Internet
Author: User

Reprint please specify the original address: http://www.cnblogs.com/ygj0930/p/6581009.html

In programming, for string concatenation we can use the string class overloaded with + or concat (str), stringbuffer append (str), StringBuilder append (str). So what is the difference between the three?

One: String

Characteristics of the 1:string class

1) string is a final class: So the string class cannot be inherited. Similarly, the value of a string object cannot be changed;

2) Comparison of creation methods of string objects (principle comparison):

When the JVM loads the run class file, the constants (symbol references, string literals, and so on) that appear in the byte file are categorized in the constant pool in the method area. Where the string literal constants that appear in the source code are saved in the Constant_string_info constant table . Then, depending on the creation method, there will be different content assignments, as follows:

1, literal constant way to create a string variable : string str1= "string";

String str2= "string";

The above creates two string references, all pointing to strings of the same content. We know that when the class is loaded, the string constants are stored in the Constant_string_info constant table , and the same string content is stored only once (we call the string in the Constant table "detention string"). Therefore, the above two string references all point to the same address (the same detention string) in the Constant_string_info constant table . So, here Str1==str2 returns True. (Another: There is no string object created because there is no memory allocated in the heap)

2. Create a string Objectwith the New keyword: string str1=new string ("string");

String Str2=new string ("string");

Class at load time, the string constants appearing in the source code are placed in the Constant_string_info constant table, so: the same "string" constant value in the above two statements is placed in the same place in the string constant table. Then, when the two statements are executed, the string objects str1 and str2 are created separately in the heap according to the new keyword, and both objects hold a value of "string" in the heap. That is, there are three places in the JVM where the same string value exists: the detention string in the constant table, the heap space of the Str1 object, and the heap space of the STR2 object. Therefore, the STR1==STR2 returned here is false. Call the string class to override the Equals () method to compare the values of two objects to return true.

3. String "Object" created by the Intern () method: String str1= "string";

String Str2=new string ("string"). Intern ();

Intern (): When there is a string in the constant pool string constant table (with the same content) as "string" (detention string), the address of the detention string is returned directly, assigned to STR2 (no new object is created at this time);

If the constant pool does not have the same detention string as "string", then "string" is added to the constant pool (becomes a detention string for return use for the next intern ()), and a string object is created in the heap and a reference to the object is returned (a new object is created at this time);

So, here Str1==str2 returns True.

Thus, theintern () method can be used to avoid the creation of duplicate content string objects , which deserves our attention.

How the + and Concat (str) works in 2:string

Two: StringBuffer

1:stringbuffer is a thread-safe final class

2:stringbuffer's Append () Working principle

Three: StringBuilder

1:stringbuilder is a non-thread-safe final class

2:stringbuilder's Append () Working principle

Four: Performance comparison and selection of three

1: Performance

2: Selection under different scenarios

String, StringBuffer, and StringBuilder comparisons in Java

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.