Java String choices

Source: Internet
Author: User

Here we analyze the connection of strings.

  1.String

Open the source string,

  

You will find that the character value that stores the string is the final constant. Looking at the construction method of string, we find that the value value of string is determined in the construction method. It is necessary to explain the following keyword final

  The final decorated property is a constant (the value is immutable), or it is assigned at the same time as the declaration, or it is assigned in the constructor method, and once the assignment cannot be changed.

So, string concatenation is implemented with string, because the value of string cannot be changed, so a new string is generated each time the concatenation is stored. Therefore, string concatenation can be used to deal with very low performance.

For more information on string, refer to the blog: http://longpo.iteye.com/blog/2199493

  2.StringBuffer

StringBuffer class inherits the abstract class Abstractstringbuilder class, open Abstractstringbuilder source code

  

Let's take a look at StringBuffer. Three construction methods for overloading

  

  

Discovery is the construction method that calls the parent class Abstractstringbuilder

  

The char array that found StringBuffer's stored data is not final type, stating that it can be changed , and that the constructed string has a space to stitch the string.

In StringBuffer we use the Append () function for concatenation of strings. We can think of that although the char array has remaining, it is certainly not enough to be spliced. So it is necessary to see the append function of the source code implementation.

  

View the Append method of its parent class Abstractstringbuilder

  

  

found that when the value array is insufficient in capacity, a new value array is created to store the string. Here you should understand the principle of stringbuffer string concatenation. When the char value array is insufficient, a larger array is created to store. Efficiency is significantly higher than string.

  3.StringBuilder

StringBuilder and StringBuffer are two brothers, the usage is basically the same, the difference is StringBuffer is synchronous, thread-safe, and StringBuilder does not guarantee synchronization, thread insecure.

  

  

  StringBuilder is faster than StringBuffer in most implementations, and it is recommended to use StringBuilder when a string buffer is accessed by a single thread

Java String choices

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.