StringBuilder - string buffer, saving the inner space variable length arrays

. 1  Package cn.learn;
 2  / * 
. 3  string buffer - the buffer can increase the efficiency
 . 4  java.lang.StringBuilder
 . 5  underlying string is a modified final array is not changed, is a constant
 . 6  Private final byte [] value;
 . 7  that is in the character string adding, generates a plurality of strings, the space occupied by low efficiency
 . 8  EG: STR string = "A" + "B" + "C"
 . 9  will be a, b, c, ab, abc five strings
 10  
. 11  and StringBuilder class, can increase the efficiency (as may be the length of a string conversion)
 12 is  byte [] = new new byte value [16]; // default length 16
 13 is  NOTE: in StringBuilder is always a memory array, a small footprint, high efficiency, if beyond the StringBuilder
 14  volume automatically expansion
 15  the bottom is not a final modification of the array can change the length
 16  
. 17 Using the StringBuilder, add data array element is not the source address, i.e., does not generate a new array
 18 is  
. 19  
20 is   * / 
21 is  public  class StringBuilderApi {
 22 is      public  static  void main (String [] args) {
 23 is          // constructor with no arguments, the default length 16, which blank 
24          the StringBuilder STR = new new the StringBuilder ();
 25          // has parameters configured, the String is actually converted the StringBuilder 
26 is          the StringBuilder str1 = new new the StringBuilder ( "cn.learn" );
 27          System.out.println ( str1);   // cn.learn
 28          // add data, the current returns the object itself may be further added append (additional), i.e., does not generate a new array
29          System.out.println (str1.append ( "huxiaobai.") The append ( "Dada.." ));
 30  
31 is  
32          // toString, can be converted to StringBuilder toString, objects can be transferred with a reference structure 
33 is          StringBuilder tostr = new new the StringBuilder (str1);
 34 is          System.out.println (toStr.toString ());
 35  
36      }
 37 [  
38 is  
39 }

 

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11488844.html