Saturday 14 January 2017

Java StringBuffer Class



 The StringBuffer Class

  The instances of the String class represent a string that cannot be modified. If you do want to create modifiable strings because you are going to do lots of string manipulation, you should use the StringBuffer class. You can create a string by passing the string value as an argument of the StringBuffer class constructor, as shown here:

  StringBuffer sb = new StringBuffer("Hello Dear!");

  You can also pass in a String reference as a variable:

  String str = "Hello Dear!";

  StringBuffer sb = new StringBuffer(str);

  You can also create an empty string and assign it a value later by modifying it:

  StringBuffer sb = new StringBuffer();

  Some methods of the StringBuffer class are presented in Table 9-2. They return the original string after modifying it.

  StringBuffer append(String str): This Method is used to appends Or Concatenate String str to the current string buffer.

StringBuffer insert(int offset, String str): This method is used to inserts String str into the current string buffer at the position specified by offset.

  StringBuffer reverse(): This method is used to reverses the characters in the current string buffer.

  StringBuffer setCharAt(int offset, char ch): This method is used to replace the character at position offset in the current buffer string with the new character specified by ch.

  StringBuffer setLength(int nlength): This method sets the new length of the current string buffer to nlength. If nlength is smaller than the length of the current string buffer, it is truncated. If nlength is greater than the current length, the null characters are added to the end of the string buffer.

  String toString(): This method return the String object that contains the sequence of characters contained by the StringBuffer on which the method is invoked. A new String object is created in this case.

  The effects of some of these methods are shown in the following code fragment:


1. StringBuffer sb = new StringBuffer("Hello");

  2. sb.reverse(); // olleH
  3. sb.insert(1, "My"); // oMylleH
  4. sb.append("ello"); // oMylleHello

  Bellow example demonstrates that when you append a string to an original string by calling a method, a new string is created and the original string remains unchanged if it was created using the String class. However, the original string is modified if it was created using the StringBuffer class. This is demonstrated by the values of the references s1, sn, and sb in the output, keeping in mind that the references still refer to the original strings because no assignments have been changed.

 StringAndBuffer.java

public class StringAndBuffer
{
  public static void main(String[] args)
  {
    String sl = "String literal!";
    String sn = new String("String new");
    StringBuffer sb = new StringBuffer ("String buffer");
    sl.concat(" Ya!");
    sn.concat(" Ya!");
    sb.append(" Ya!");
  System.out.println("sl after concat(): " + sl);
    System.out.println("sn after concat(): " + sn);
    System.out.println("sb after append(): " + sb);
  }
}

  The output is as following :
    sl after concat(): String literal!
    sn after concat(): String new
    sb after append(): String buffer Ya!

  The output demonstrates that only sb refers to the concatenated string, while s1 and sn keep referring to the old strings. That means the concat operation on s1 and sn created new concatenated strings, whereas the append operation on sb modified the old string itself.
  Unlike the String class, the StringBuffer class does not override the equals(…) method of the Object class. Thus, it would return true only if the two object references are referring to the same string buffer object.
  Because the StringBuffer class, unlike the String class, is designed to be thread safe (i.e. multiple threads can use a StringBuffer string in a synchronized way), you pay some price in terms of performance. If you know that your application is only a single-threaded application, you can use the StringBuilder class (which has the same functionality as the StringBuffer class), which will somewhat improve the performance but will not guarantee synchronization


0 comments:

Post a Comment

Powered by Blogger.

Stats