Thursday, 25 December 2014

What is StringBuffer class ?


The StringBuffer class is used to created mutable (modifiable) string.The StringBuffer class is same as String except it is mutable i.e. it can be changed.

Note: StringBuffer class is thread-safe means multiple threads cannot access it simultaneously.So it is safe and will result in a proper order whiling acccessing by multiple thread.

Commonly used Constructors of StringBuffer class:

>1. StringBuffer(): creates an empty string buffer with the initial capacity of 16.
>2. StringBuffer(String str): creates a string buffer with the specified string.
>3. StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.


Commonly used methods of StringBuffer class:



>1. public synchronized StringBuffer append(String s): is used to append the specified string with      this string. The append() method is overloaded like append(char), append(boolean), append(int),
      append(float), append(double) etc.
>2. public synchronized StringBuffer insert(int offset, String s): is used to insert the specified              string with this string at the specified position. The insert() method is overloaded like
      insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
>3. public synchronized StringBuffer replace(int startIndex, int endIndex, String str): is used to
      replace the string from specified startIndex and endIndex.
>4. public synchronized StringBuffer delete(int startIndex, int endIndex): is used to delete the
      string from specified startIndex and endIndex.
>5. public synchronized StringBuffer reverse(): is used to reverse the string.
>6. public int capacity(): is used to return the current capacity.
>7. public void ensureCapacity(int minimumCapacity): is used to ensure the capacity at least              equal to the given minimum.
>8. public char charAt(int index): is used to return the character at the specified position.
>9. public int length(): is used to return the length of the string i.e. total number of characters.
>10. public String substring(int beginIndex): is used to return the substring from the specified
       beginIndex.
>11. public String substring(int beginIndex, int endIndex): is used to return the substring from the
     specified beginIndex and endIndex.

Que) What is mutable string ?

Ans: A string that can be modified or changed is known as mutable string. StringBuffer and                        StringBuilder classes are used for creating mutable string.

An example of StringBuffer class by append() method:

The append() method concatenates the given argument with this string.

class Test{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("vishwa ");
sb.append("welcome");
System.out.println(sb);
   }
}

Output: vishwa welcome

Example of insert() method of StringBuffer class:

The insert() method inserts the given string with this string at the given position.

class Test{
public static void main(String []args){
StringBuffer sb=new StringBuffer("vishwa ");
sb.insert(2,"hello");
System.out.println(sb);
   }
}

Output: vihelloshwa

Example of replace() method of StringBuffer class:

The replace() method replaces the given string from the specified beginIndex(inclusive) and endIndex(exclusive).

class Test{
public static void main(String []args){
StringBuffer sb=new StringBuffer("vishwa");
sb.replace(1,4,"hello");
System.out.println(sb);
   }
}

Output:vhellowa



Example of delete() method of StringBuffer class:

The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.

class Test{
public static void main(String []args){
StringBuffer sb=new StringBuffer("vishwa");
sb.delete(1,4);
System.out.println(sb);
  }
}


Output: vwa

Example of reverse() method of StringBuffer class:

The reverse() method of StringBuilder class reverses the current string.

class Test{
public static void main(String []args){
StringBuffer sb=new StringBuffer("vishwa");
sb.reverse();
System.out.println(sb);
   }
}

Output: awhsiv

Example of capacity() method of StringBuffer class:

The capacity() method of StringBuffer class returns the current capacity of the buffer.The default capacity of the buffer is 16.
If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your  current capacity is 16, it will be (16*2)+2=34.

class Test{
public static void main(String []args){

StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());
sb.append("vishwa");
System.out.println(sb.capacity());
sb.append("my name is vishwa");
System.out.println(sb.capacity());
   }
}

Output:  16  (by default)
                16
                34


No comments:

Post a Comment