JAVA StringBuffer and StringBuilder classes

Source: Internet
Author: User

When you modify a string, you need to use the StringBuffer and StringBuilder classes.

Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified more than once, and no new unused objects are produced.

The StringBuilder class is presented in Java 5, and the biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously).

Since StringBuilder has a speed advantage over StringBuffer, it is recommended to use the StringBuilder class in most cases. However, in cases where the application requires thread safety, the StringBuffer class must be used.

StringBuilder Thread not secure Fast speed
StringBuffer Thread Safety Slow speed
 Public class test{  publicstaticvoid  main (String args[]) {    new StringBuffer ("Novice Tutorial official Website:");    Sbuffer.append ("www");    Sbuffer.append (". Runoob");    Sbuffer.append (". com");    System.out.println (Sbuffer);    }}

The results of the above example compilation run as follows:

Novice Tutorial Official website: www.runoob.com
StringBuffer Method

The following are the main methods supported by the StringBuffer class:

The methods in the following list are similar to the methods of the String class:

JAVA StringBuffer and StringBuilder classes

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.