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