The length of the successful string object is fixed, and the content is that it cannot be modified and edited. Although "+" can achieve the purpose of adding new characters or string, "+" will generate a new String instance and create a new string object in memory. If the string is repeatedly modified, it will greatly increase the system overhead. J2SE has increased the variable character sequence String-Builder class from 5.0, which greatly improves the efficiency of frequent increased string. Let's look at a simple example below.
Public Class Jerque { / *** The string generator in general, and the time consumed by String-Builder* / Public Static Void Main (String [] ARGS) {// Todo Auto-Generatd Method Stub String S tr = ""; Long Starttime1 = System.currenttTimemillis (); for (Int I = 0; I <10000; I ++) {str = str+i; long time1 = endTime1 -Starttime1; System .out.println ("String 1 consumes time:"+ time1); StringBuilder Builder = New StringBuilder (""); Long Starttime2 = SYSTEM.CURRENTTTTTTIMILIS (); for (INT I = 0; <10000; i ++) { Builder.append (i);} Long endtime2 = System.CURRENTTTTTIMILLIS (); Long Time2 = Endtime2 -Starttime2; System.out.println ("" Strings 2 consuming time: " + Time2) ;}}
String 1 consumption of time: 1210
String 2 consumption time: 3
Summarize:
1. The String-Builder class that comes with Java comes with Java has undoubtedly greatly improved efficiency.
2. This commonly used method is as follows:
a. APEND (Content) method
b. Insert (int Office, ARG) method
StringBuilder B = New StringBuilder ("Hello");
b.Insert (5, "world!");
System.out.println (b.tostring ());
c. Delete (int Start, int End) method
StringBuilder D = New StringBuilder ("StringBuilder");
D.DELETE (5,10);
System.out.println ("d.tostring ()");