Easy Tutorial
For Competitive Exams

Java Programming StringBuffer

What is StringBuffer?

The StringBuffer class is used to represent characters that can be modified.

StringBuffer is mainly used for the dynamic string concatenation which enhances the performance.

A string buffer implements a mutable sequence of characters.

A string buffer is like a String, but can be modified.

When StringBuffer is used?

StringBuffer class is used when we have to make lot of modifications to our string.

It is also thread safe i.e multiple threads cannot access it simultaneously.

Methods of StringBuffer:

How to use append()?

This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object.

Example:

public class Demo{
   public static void main(String args[]){
      StringBuffer sb=new StringBuffer("Fruits");
      sb.append("are good for health");
      sb.append(123);
     System.out.println(sb);
  }
}
Try it Yourself

How to use insert() ?

This method inserts one string into another.

Example:

public class Demo{
   public static void main(String args[]){
     StringBuffer sb=new StringBuffer("Fruitsgood");
      sb.insert(6,"are");
     System.out.println(sb);
   }
}
Try it Yourself

How to use reverse() ?

This method reverses the characters within a StringBuffer object.

Example:

public class Demo{
   public static void main(String args[]){
     StringBuffer sb=new StringBuffer("fruits");
     sb.insert(6,"are");
     System.out.println("reverse:"+sb.reverse());
   }
}
Try it Yourself

How to use replace() ?

This method replaces the string from specified start index to the end index.

Example:

public class Demo{
   public static void main(String args[]){
     StringBuffer str = new StringBuffer("Hello World");
     str.replace( 6, 11, "java");
    System.out.println(str);
   }
}
Try it Yourself

How to use capacity() ?

This method returns the current capacity of StringBuffer object.

Example:

public class Demo{
    public static void main(String args[]){
       StringBuffer str = new StringBuffer();
       System.out.println( str.capacity() );
    }
}
Try it Yourself

How to use ensureCapacity() ?

This method is used to ensure minimum capacity of StringBuffer object.

Example:

public class Demo{
    public static void main(String args[]){
      StringBuffer str = new StringBuffer("hello");
      str.ensureCapacity(10);
   }
}
Try it Yourself

How to use delete() ?

This method delete data from string.

Example:

public class Demo{
    public static void main(String args[]){
        StringBuffer sb=new StringBuffer("Fruits are very good");
        sb.delete(10,15);
        System.out.println("After delete:"+sb);
 }
}
Try it Yourself

Solve this!!

21182.Which of these class is used to create an object whose character sequence is mutable?
String
StringBuffer
Both of the mentioned
None of the mentioned
21183.What is the output of this program?
    class output {
        public static void main(String args[]){ 
           StringBuffer s1 = new StringBuffer("Hello");
           StringBuffer s2 = s1.reverse();
           System.out.println(s2);
        }
    }
Hello
olleH
HelloolleH
olleHHello
Explanation:
reverse() method reverses all characters. It returns the reversed object on which it was called.
21184.What is the output of this program?
    class output {
        public static void main(String args[])
        { 
             StringBuffer c = new StringBuffer("Hello");
             StringBuffer c1 = new StringBuffer(" World");
             c.append(c1);
             System.out.println(c);
        }
    }
Hello
World
Helloworld
Hello World
Explanation:
append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
21185.What is the output of this program?
    class output {
        public static void main(String args[]){ 
             StringBuffer c = new StringBuffer("Hello");
             c.delete(0,2);
             System.out.println(c);
        }
    }
He
Hel
lo
llo
Explanation:
delete(0,2) is used to delete the characters from 0 th position to 1 st position.
Share with Friends