Java Learning (18th), String, StringBuffer, and StringBuilder define a class of their own StringBuilder, bufferbuilder

Source: Internet
Author: User

Java Learning (18th), String, StringBuffer, and StringBuilder define a class of their own StringBuilder, bufferbuilder

String

The String value in Java belongs to the String class. Although there are other methods to represent strings (such as character arrays), Java generally uses the String class as the standard format of strings, the Java compiler uses the String value as a String object;

Once created, the String object cannot be changed. If you need to modify a large number of strings, use the StringBuilder/StringBuffer class or String array to convert the final result to a String object;

 

StringBuffer: Thread-safe variable character sequence;

A String buffer, similar to a String, can be called in some ways to change the length and content of the sequence;

Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated;

If the memory buffer overflows, the capacity increases automatically;

 

StringBuilder: From JDK1.5, The StringBuffer class is supplemented with an equivalent class used by a single thread, that is, StringBuffer;

Compared with StringBuffer, The StringBuilder class should be preferred, because it supports all the same operations, but because it does not execute synchronization, all the faster;

Common Methods:

Public int length ()

Public int capacity ()

Public StringBuffer append (String str)

Public StringBuffer insert (int offset, String str)

Public int indexOf (String str)

Public int indexOf (String str, int fromIndex)

Public int lastIndexOf (String str)

Public StringBuffer reverse ()

Public String toString ()

1 public class StringBuilderDemo {2 public static void main (String [] args) {3 // StringBuilder sb = "abc "; // no such declaration method 4 // StringBuilder sb = new StringBuilder (); // The default capacity of 16 characters is 5 // StringBuilder sb = new StringBuilder (100 ); // initialize the dynamic string of capacity 6 // StringBuilder sb = new StringBuilder ("abc"); 7 8 StringBuilder sb = new StringBuilder (); 9 sb. append ("hello"); 10 sb. append (1); 11 sb. append (1, 1.5); 12 sb. append (true); 13 System. out. println (sb. length (); 14 System. out. println (sb. capacity (); 15 16 sb. insert (5, "world"); 17 System. out. println (sb. toString (); 18 sb. replace (5, 7, "el"); 19 System. out. println (sb. toString (); 20 System. out. println (sb. indexOf ("el"); 21 System. out. println (sb. reverse (); 22} 23}View Code

 

Customize a StringBuilder class

Case study: Customize a MyStringBuilder class to implement the StringBuilder function;

Case Design: append a string using the append method, calculate the number of characters using the length method, obtain the container size using the capacity () method, and implement the toString () method to output strings;

1 import java. util. arrays; 2 public class MyStringBuilderDemo {3 public static void main (String [] args) {4 MyStringBuilder msb = new MyStringBuilder (); 5 msb. append ("hello "). append ("java "). append ("12345678"); 6 System. out. println ("number of characters:" + msb. length (); 7 System. out. println ("total capacity:" + msb. capacity (); 8 System. out. println ("output string:" + msb. toString (); 9} 10} 11 12 class MyStringBuilder {13 private char [] value; // character array 14 private int count = 0; // number of characters in the character array 15 public MyStringBuilder () {16 value = new char [16]; 17} 18 public MyStringBuilder (int capacity) {19 value = new char [capacity]; 20} 21 public MyStringBuilder (String str) {22 value = new char [str. length () + 16]; 23} 24 // obtain the number of characters in the character array 25 public int length () {26 return count; 27} 28 // return container capacity size 29 public int capacity () {30 return value. length; 31} 32 // Add (append) 33 public MyStringBuilder append (String str) {34 int len = str. length (); // obtain the length of the string to be added 35 // ensure that the character array can be put into the added string 36 ensureCapacity (count + len ); 37 // append the string to be added to the specified position of the new specified array after 38 str. getChars (0, len, value, count); 39 count + = len; // The number of elements increases by len40 return this; 41} 42 43 private void ensureCapacity (int capacity) {44 // data exceeds capacity size 45 if (capacity-value.length> 0) {46 int newCapacity = value. length * 2 + 2; // the size of the expanded new character array 47 value = Arrays. copyOf (value, newCapacity); 48} 49} 50 // convert the character array to a String in the format of 51 public String toString () {52 return new String (value, 0, count); 53} 54}

 

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.