The difference and connection between string,stringbuffer,stringbuilder

Source: Internet
Author: User
Tags stringbuffer
Tring is a string constant. StringBuffer is a string variable, thread-safe. StringBuilder is a string variable and the thread is not secure.

Speed: String<stringbuffer<stringbuilder

Why is string speed the slowest?

Because each time a string type is changed, it is equivalent to generating a new string object and then pointing the pointer at a new string object, so that the old object accumulates in memory when the new object is constantly generated, causing memory waste, The Java GC garbage collector reclaims old objects, slowing the program down severely. And StringBuilder is different, each operation is on the same object, and StringBuilder do not consider thread synchronization (StringBuffer is thread-safe, to consider thread synchronization), so StringBuilder fastest.


Many articles are recommended for use with StringBuilder, because StringBuilder is the fastest and most programs are single-threaded.

The StringBuilder and StringBuffer methods are basically the same, and the following examples give only string and StringBuffer tests.

[Java]  View plain copy package com.xujin;      public class test     {        public static void main (String []  args) {            String a =  "this "  +  "is a simple "  +  "example.";            system.out.println (a);            stringbuilder sb = new stringbuilder ();           sb.insert (0, true);            system.out.println (SB);//true            sb.insert (0,  "Hello,");            System.out.println (SB);//hello,true   &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;   sb.setcharat (2,  ' o ');            System.out.println (SB);//heolo,true           sb.reverse ();            system.out.println (SB);//eurt,oloeh           system.out.println (Sb.capacity ());//16            sb.trimtosize ();            system.out.println (Sb.length ());//10       }             }   


Use Policy:

1, if the string does not add and subtract operation, use String.

2, if the string on the thread safety requirements, the use of StringBuffer.

3, if the string for thread safety does not require, and the pursuit of efficiency, the use of StringBuilder. Recommended use of StringBuilder.

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.