Differences between StringBuffer and StringBuilder in Java _java

Source: Internet
Author: User
Tags stringbuffer

Earlier in Java, it was known that there was a class called StringBuffer, which was used to stitch longer strings. After you go to C #, there is also a class-like function called StringBuilder, shorthand is SB, very good to remember.

Then later transferred back to Java, and found that Java also has a StringBuilder, so I was curious why stringbuffer after the launch of the StringBuilder.

The original Java StringBuilder (as C #) is not thread-safe, and earlier StringBuffer has certain thread-safe properties. Of course, the introduction of StringBuilder, mainly because it is not necessary in the context of multithreading to use.

Common StringBuilder (or StringBuffer) use cases are:

Public String toString () {return
 new StringBuilder ()
  . Append ("Name:" + name)
  . Append ("foo:" + foo)
  . AP Pend ("bar:" + bar)
  . toString ();

In this case, StringBuilder is not a class member, it is only a local variable, there is no problem of multithreading.

As a result, the introduction of StringBuilder brings a very large performance boost, and there is no security problem ...

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.