Several ways to concatenate Java strings

Source: Internet
Author: User

several ways to concatenate Java strings Several ways of string representation StringBuffer and StringBuilder and string inheritance relations

Connection of Strings

Connection method for 1.String


You can see that the connection is made by creating a new two-length character array and then connecting.

Storing strings in 2.StringBuilder its practical is a char array, capacity is actually specifying the size of this char array, The StringBuilder connection method is inherited by the Abstractstringbuilder method, and the thread is unsafe.


When the append (str) function is called, it is first judged that the string array of values used to store the string has not enough size to store the string that will be added to the StringBuilder. If not enough, then call expandcapacity (int minimumcapacity) to double the capacity (typically twice times the expansion)

3.StringBuffer connection method, using the synchronization (synchronized keyword), thread-safe, but this will be relatively slow down.


4. "+" number stitching

When using + splicing, the + sign will be transformed into StringBuilder append mode, that is, each use + will establish a StringBuilder. Therefore, inside the loop means that every time a loop is executed, a StringBuilder object is created. So do not use the + sign inside the loop to build a StringBuilder or Stirngbuffer outside the loop.

Several ways to concatenate Java strings

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.