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:
- charAt (index); // get the corresponding underlying index characters
- length (); // Returns the size of the character array, an array with a length attribute String length () method, with the collection class size () method;
- contains (str); // returns boolean, it is included respective substring;
- getBytes (); // returns a byte array;
- toCharArray (); // converted into an array of characters;
- indexOf (char); // Find the position of the specified element in the first occurrence of the string;
- lastIndexOf (char); // location to find the last occurrence of an element;
- repalce (char, char); // replace the specified character;
- split (string); // string is divided in accordance with the string, the result is an array str;
- subString (int): cut the string: the last subString (int, int) when cut from the index string to int [) interval;
- toUperCase (): turn all capital letters;
- toLowerCase (): turn all lowercase letters;
- trim (); // removing spaces around the string;
- 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:
- append(object); //拼串;
- capacity (); // returns the length of the underlying array of characters;
- length (); // return a valid number of elements;
- charAt (int); // index position by index returned char;
- subString (int, int); // Returns a String [)
- deleteCharAt (int) l // by index delete a character, returns a StringBuilder;
- setCharAt (int, char); // index of the giant character modification;
- reverse (); // return a string or a flip the StringBuilder;
- toString (); // will return a String into StringBuilder;
- 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.