The difference between String StringBuilder StringBuffer

Source: Internet
Author: User

1.String is the final type and cannot be inherited is immutable

StringBuffer and StringBuilder can be changed.

2. Speed stringbuilder>stringbuffer>string

StringBuffer is thread-safe

Stringbuidler is thread-insecure

StringS1=“ThisIsOnlyA "+ " simple " +  "test" ; StringBuffer sb = new stringbuilder  ( "this is only a ") . Append ( "simple" ) append ( "test"                

Java compiler directly compiles the first statement above to:
String s2 = “This is only a”; String s3 = “ simple”; String s4 = “ test”; String s1 = s2 + s3 + s4;

3. Using policies

1) A small number of data operations can be string, single-threaded operation with Sringbuilder, multi-threaded operation with StringBuffer

2) do not use string "+" to splice, so the performance is very poor, you should use the following second:

Stringresult= "";  for(Strings: Hugearray) { result= result+   s   ;   }    stringb Uilder sb = new stringbuilder (); for  (string s :  Hugearray) {sb. Append (s} string result = sb tostring ()             

3) StringBuffer used in global variables; StringBuilder is used inside the method

3) generally use StringBuffer directly, as Builder's performance increases by up to 10%-15%, unless the acknowledgement is not running under multithreading and the performance bottleneck is on buffer.

The difference between String StringBuilder StringBuffer

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.