Java implementation simple Word wildcard Builder code example _java

Source: Internet
Author: User

The

Creates a successful string object whose length is fixed and cannot be modified and edited. Although you can use "+" to add new characters or strings, "+" produces a new instance of string that creates new strings objects in memory. If you repeatedly modify the string, you will greatly increase the overhead of the system. J2SE has increased the String-builder class of variable character sequences from 5.0, greatly increasing the efficiency of increasing strings frequently. Let's look at a simple example.

public class Jerque { 
 
  /** 
   * Comparison of generic string generators, and string-builder elapsed time * 
   * public 
  static void Main (string[) args) { 
    //TODO auto-generated method stub 
    String str = ""; 
    Long startTime1 = System.currenttimemillis (); 
    for (int i =0; i<10000; i++) 
    { 
      str = str +i;   
    } 
    Long endTime1 = System.currenttimemillis (); 
    Long time1 = endtime1-starttime1; 
    System.out.println ("String 1 Consumes Time:" + time1); 
     
    StringBuilder builder = new StringBuilder (""); 
    Long startTime2 = System.currenttimemillis (); 
    for (int i=0;i<10000;i++) 
    { 
      builder.append (i); 
    }   
    Long endTime2 = System.currenttimemillis (); 
    Long time2 = endtime2-starttime2; 
    System.out.println ("String 2 consumes Time:" + time2); 
  } 

String 1 consumption time: 1210
String 2 consumption time: 3

Summarize:

1. The String-builder class with Java-brought character sequences undoubtedly greatly improves the efficiency.  
2. Such commonly used methods are as follows: 
A. Append (content) method  
B. Insert (int offset,arg) method  
   S Tringbuilder B = new StringBuilder ("Hello"); 
   B.insert (5, "world!");  
   System.out.println (b.tostring ()); 
C. Delete (int start,int end) method  
 & nbsp StringBuilder d = new StringBuilder ("StringBuilder"); 
   d.delete (5,10); 
   System.out.println ("d.tostring ()");  

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.