The interesting Java String, StringBuffer and StringBuilder

 String Introduction

  String category belongs java.lang package, String class is immutable class, any change to the initiator generates a new String String object.

  Create a String of two ways:

  1. Create a constructor by: STR = String new new String ( "the I AM A String.");

  2. With variable assignment: String = STR "the I AM A String";

  The first will be initialized in the heap ( heap create a String object), and then reference the object returned to the user.

  The second way, JVM will first (string constant pool) to determine whether the object exists in the String Pool, if it already exists, it will assign the address of an existing string variable; will run the program if you do not exist, string into the constant pool at its address assigned to the variable.

  String of common methods

  Intern () : Intern method returns a constant value corresponding to the string. Intern in the implementation of the method, JVM checks for the string and the same constant value constant pool, if there is, the constant value is returned, if not, create the constant value and returns. That is, intern returns String value of the constant pool, not a heap of String, String to create the equivalent of double quotation marks. 

        String str1 = "abc";
        String str2 = "abcd";
        String str3 = "abc" + "d";
        String str4 = (str1 + "d").intern();

        System.out.println(str2 == str3);//true
        System.out.println(str2 == str4);//true

   length (): Get string length length () method

   charAt (): Get the string in the i-th character the charAt method (i)

    char CH = str.charAt (I); // I is the index of the string, the character string can be obtained anywhere, save the character variable

   getChars (): Get the specified character positions a method getChars (4 parameters)

  char Array [] = new new  char [80];   // first create a capacity large enough to char array, an array called Array 
  str.getChars (indexBegin, indexEnd, Array, arrayBegin);
  / * 
  . 1, indexBegin: requires the starting index of the string copied 
  2, indexEnd: need to copy the string end index,. 1-indexEnd 
  . 3, array: the array name char array defined above 
  4, arrayBegin: array An array storage start position of index 
  * /

   getBytes (): for internet String performed with the default which has been encoded, and stores the result in a new byte array, similar getChars ().

   the equals (): the equals (Object object) String comparison of source and object contents are equal.

   equalsIgnoreCase (): Use similar equals (), but the comparison ignoring case.  

   the compareTo (): the compareTo (String anotherString), two lexicographically compare the size of a String.

   the contains (): the contains (CharSequence S), determines whether it contains the source String s. Returns 1 comprises, excluding 0 is returned.

   regionMatches (): regionMatches (boolean ignoreCase, Toffset int, String OTHER, ooffset int, int len). IgnoreCase first parameter indicates whether to ignore the size comparison, Comparison begins toffset String subscripts and subscript starting ooffset String other are equal, len indicates the length of the specified comparison.

   startsWith (): startsWith (String prefix) determines whether to begin with prefix, it is returns true, and vice versa, false is returned

   endsWith (String suffix): determines whether the ending prefix, is returns true, and vice versa, false is returned  

   indexOf (): indexOf (int ch), from left to right to find the ASCII code for the character ch, if the index character is returned. If not, it returns -1

   lastIndexOf (): lastIndexOf (int ch) from right to left to find the ASCII code for the character ch, if the index character is returned. If not, it returns -1.

   the substring (): the substring (int beginIndex, int endIndex), returns the String subscript index from beginIndex to endIndex 1-string between. 

   concat (): splicing two strings   

   Replace (): String Replace (oldChar char, char newChar), from this prototype function it can be seen that the String to replace it with newChar oldChar

   toCharArray (): converts a String into char array.

   toUpperCase () and toLowerCase (): transfer to upper / lower case

   trim (): After a blank character String both ends removed, returns a new String object. If not changed, the original String object is returned.

   valueOf (): returns a parameter indicating the content of the String, parameters may be double, int, float, char, char [], long ah like, basic can. Actually cast it! To convert other types of data into a String.

  Characteristics of the String class

  Analyzing equal 1.String class object using equals () method is completed, the "==" is achieved numerical comparison address.
  2.String class is immutable (Final), and any changes to the String class are class returns a new String object. String this case the reference is passed to a class method, any change in the method of the String of the original reference point of the object has no effect. There are two instances of class 3.String manner using direct assignment may not produce garbage space and automatically into the pool, do not use the method of construction is completed.

  important point:

  When String string concatenation, such as: String str = "ab" + "cd" + "ef";

  In theory: First, it will generate ab objects, and then generate abcd objects, the last generation abcdef object code less efficient

  Actual operation: the compiler will automatically optimize this line of code by StringBuilder way, only one object generation

  StringBuffer 

  StringBuffer is a variable type, its meaning any change in the string generation will not produce a new object, the thread-safe.

No. The method described
1 public StringBuffer append (String s)
specified string to this character sequence.
2 public StringBuffer reverse ()
 this character sequence replaced by the reverse form.
3 public delete (int start, int end )
to remove this sequence substring of characters.
4 public insert (int offset, int i )
the  int string representation of the argument into this sequence.
5 replace (int start, int end, String str)
given  String characters replacing substrings of this sequence of characters.

  StringBuilder

  StringBuilder java.lang class is the package,

  StringBuilder variable type, linear unsafe, does not support concurrent operations, it is not suitable for use in multi-threaded, but its performance is higher than in the single-threaded StringBuffer

  Comparison between the three

  Comparison of the three in terms of execution speed: StringBuilder> StringBuffer> String

  Security: String class is immutable, it is thread-safe for all immutable class are thread safe.

      StringBuffer is mutable class, achieved by locking thread-safe

      StringBuilde is not thread-safe, but the highest single-threaded execution efficiency

Guess you like

Origin www.cnblogs.com/xywl-bky/p/11712030.html