StringBuffer and StringBuilder in Java

Source: Internet
Author: User

What are/*java.lang.stringbuffer;java.lang.stringbuilder;1.stringbuffer and StringBuilder? is a string buffer. 2. How to apply a space in memory beforehand to accommodate a sequence of characters, and automatically expand to accommodate more character sequences if the reserved space is not sufficient. 3.stringbuffer,stringbuilder   and   string the biggest difference? A string is an immutable character sequence that stores strings in a constant pool. StringBuffer the bottom layer is a char array, but the char array is mutable. and can automatically expand the capacity. The default initialization capacity for 4.StringBuffer and StringBuilder is 165. How do I optimize StringBuffer and StringBuilder? It is a good idea to predict the number of stringbuffer stored characters before creating the StringBuffer, and then create the StringBuffer by specifying the initial capacity to create the stringbuffer. To reduce the copy of the underlying array. Improve efficiency. What is the difference between 6.StringBuffer and StringBuilder? StringBuffer is thread-safe. (You can use it in a multithreaded environment without problems.) StringBuilder is non-thread safe. (use in a multithreaded environment may be problematic.) */public class test01{public static void main (String[] args) {// Creates a string buffer object Stringbuffer sb = new stringbuffer ();  //16//can append strings to StringBuffer. String[]  ins = {"Sports", "Music", "Sleeping", "gourmet"};//recommended strings are frequently spliced using StringBuffer or stringbuilderfor (int i=0;i< ins.length;i++) {if (i==ins.length-1) {sb.append (ins[i]);} Else{sb.append (Ins[i]); Sb.append (",");}} System.out.println (SB);}} 


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1662499

StringBuffer and StringBuilder in Java

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.