StringBuffer class of common

String StringBuffer class and as also used represents a string . But because internal implementations and String StringBuffer different, when performing StringBuffer string processing, does not generate a new object in a memory using the superior class String .

  So in actual use, if you often need a string modify, insert, delete and other operations, use StringBuffer to be more suitable for some.

  Many, like the String class and there is a method in StringBuffer class, these methods in the String class functions and features are exactly the same. However, StringBuffer object modification will change the object itself , which is the point and the biggest difference between the String class .

  In addition, as StringBuffer is thread-safe, so in a multithreaded program can also be very convenient for use, but the efficiency of the program would be relatively little slower.

 1, StringBuffer object initialization

  Constructor initializes generally used. E.g: 

  StringBuffer s = new StringBuffer (); // initialize out such StringBuffer object is an empty object.

  StringBuffer s = new StringBuffer ( "abc"); // content of such an initialization is StringBuffer object string "abc".

  It should be noted, the StringBuffer and String are of different types, can not be directly cast, the following code is wrong:

         StringBuffer s = "abc"; // assignment type mismatch

         StringBuffer s = (StringBuffer) "abc"; // there is no inheritance, can not be strong turn

    System conversion between the code and the objects StringBuffer String object as follows:

         String s = “abc”;

     StringBuffer s2sb = new StringBuffer(s);   //String转换为StringBuffer

         StringBuffer sb = new StringBuffer(“123”);         

         String sb2s = sb.toString (); // StringBuffer to a String

  2, StringBuffer common method

  Method StringBuffer class for changes in the main emphasis on the string, for example, other main difference is added, insert and delete, and this is also StringBuffer String class.

  a, append method

  public StringBuffer append(boolean b)

      Effect of this method is appended to the end of the current StringBuffer object, similar to the connection string. After calling this method, the contents of the StringBuffer object is also changed,

  E.g:

          StringBuffer sb = new StringBuffer(“abc”);

          sb.append (true); // the value of the object will become sb "abctrue".

      For another example:

    The method for using the connection string, more economical than String content, e.g. SQL statements applied to the database is connected, for example:

      StringBuffer sb = new StringBuffer();

      String user = “test”;

      String pwd = “123”;

      sb.append(“select * from userInfo where username=“)

             .append(user)

             .append(“ and pwd=”)

             .append(pwd);

          Such a target value sb is the string "select * from userInfo where username = test and pwd = 123".

 

   b, deleteCharAt method

       public StringBuffer deleteCharAt(int index)

      This method is to remove the effect of the position of the specified character, then the remaining contents of the new string is formed. E.g:

          StringBuffer sb = new StringBuffer(“Test”);

          sb deleteCharAt (1);. // Object sb value becomes "Tst"

        public StringBuffer delete(int start,int end)

      The role of this method is to delete all the characters within the specified range, include start, does not contain a range of end index values. E.g:

          StringBuffer sb = new StringBuffer(“TestString”);

          . Sb delete (1,4); // target value sb is "TString

 

 c, insert method

      public StringBuffer insert(int offset, boolean b)

           The effect of the method is to insert the contents StringBuffer object, then a new string. E.g:

           StringBuffer sb = new StringBuffer(“TestString”);

           sb.insert (4, false); // target values ​​sb is "TestfalseString"

 

 d, reverse method

      public StringBuffer reverse()

      The effect of this method is the reverse StringBuffer object content, then a new string. E.g:

          StringBuffer sb = new StringBuffer(“abc”);

          sb.reverse (); // After later reversed, the contents of the object sb will become "cba"

 

 e, setCharAt method

  public void setCharAt(int index, char ch)

           The method is to modify the action of the character object index value index to a new character ch. E.g:

           StringBuffer sb = new StringBuffer(“abc”);

           sb.setCharAt (1, 'D'); // the target value becomes sb "aDc"                           

  

 f, trimToSize method

       public void trimToSize()

        The effect of this method is the reduced storage space StringBuffer object to the same length as the length of the string and to reduce the waste of space.

            In short, in actual use, String and StringBuffer have their own advantages and disadvantages, depending on the specific use of the environment, select the corresponding types use

Guess you like

Origin www.cnblogs.com/chenjiajiale/p/12593768.html