学习2____String,StringBuilder,StringBuffer

A: String:

String class immutable reasons: the bottom by private final char [] array character modification, which also determines the String class can not be inherited (final modification); 

    1. The method of construction: since the different (constant pool, the stack) stored in the memory mode, String constants can be assigned.

// constructor in two ways: 
String STR = "123";    // reference type, but can be assigned to the same constant, because of the way stored in memory is not the same 
String STR = new new String ( "123");

   2. Common methods:

  1. charAt (index); // get the corresponding underlying index characters
  2. length (); // Returns the size of the character array, an array with a length attribute String length () method, with the collection class size () method;
  3. contains (str); // returns boolean, it is included respective substring;
  4. getBytes (); // returns a byte array;
  5. toCharArray (); // converted into an array of characters;
  6. indexOf (char); // Find the position of the specified element in the first occurrence of the string;
  7. lastIndexOf (char); // location to find the last occurrence of an element;
  8. repalce (char, char); // replace the specified character;
  9. split (string); // string is divided in accordance with the string, the result is an array str;
  10. subString (int): cut the string: the last subString (int, int) when cut from the index string to int [) interval;
  11. toUperCase (): turn all capital letters;
  12. toLowerCase (): turn all lowercase letters;
  13. trim (); // removing spaces around the string;
  14. matches (String regex); // string matches one;

二,StringBuffer,StringBuilder:

The bottom is an array of characters, different character array modifiers compared to a String, so different restrictions.

1. Constructors:

SB = StringBuilder new new StringBuilder ();  
 // no parameters configured, the default size of 16 

StringBuilder SB = new new StringBuilder ( "ABC" ); 

StringBuilder SB = new new StringBuilder (String);
 // a String object as a constructor StringBuilder parameter

2. Common methods:

  1. append(object);   //拼串;
  2. capacity (); // returns the length of the underlying array of characters;
  3. length (); // return a valid number of elements;
  4. charAt (int); // index position by index returned char;
  5. subString (int, int); // Returns a String [)
  6. deleteCharAt (int) l // by index delete a character, returns a StringBuilder;
  7. setCharAt (int, char); // index of the giant character modification;
  8. reverse (); // return a string or a flip the StringBuilder;
  9. toString (); // will return a String into StringBuilder;
  10. trimToSize (); // string compression

3. Since no StringBuilder Compareable interfaces implemented without the compareTo () method, the element can not compare;

StringBuffer is relatively early JDK version is thread-safe, but low efficiency.

 

Guess you like

Origin www.cnblogs.com/xbfchder/p/11403188.html