Easy Tutorial
For Competitive Exams

Java Programming StringBuilder

What is StringBuilder?

Java StringBuilder is identical to StringBuffer except for one important difference it is not synchronized ,that means it is not thread safe.

We should prefer StringBuilder in all cases where you have only a single thread accessing your object.

StringBuilder Methods:

The important methods in StringBuilder Class are append() , insert() , replace() , delete() , reverse() , and capacity() .

How to use append()?

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

Example:

public class Demo{
   public static void main(String args[]){
      StringBuilder sb=new StringBuilder("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[]){
     StringBuilder sb=new StringBuilder("Fruitsgood");
      sb.insert(6,"are");
     System.out.println(sb);
   }
}
Try it Yourself

How to use reverse() ?

This method reverses the characters within a StringBuilder object.

Example:

public class Demo{
   public static void main(String args[]){
     StringBuilder sb=new StringBuilder("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[]){
     StringBuilder str = new StringBuilder("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 StringBuilder object.

Example:

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

How to use ensureCapacity() ?

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

Example:

public class Demo{
    public static void main(String args[]){
      StringBuilder str = new StringBuilder("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[]){
        StringBuilder sb=new StringBuilder("Fruits are very good");
        sb.delete(10,15);
        System.out.println("After delete:"+sb);
 }
}
Try it Yourself

Solve this!!

21178.Which is not-thread safe?
StringBuffer
StringBuilder
All
None
21179.What is the output of this program?
    class output {
        public static void main(String args[]){ 
           StringBuilder s1 = new StringBuilder("Hello");
           StringBuilder 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.
21180.What is the output of this program?
    class output {
        public static void main(String args[]){ 
             StringBuilder c = new StringBuilder("Hello");
             StringBuilder c1 = new StringBuilder(" World");
             c.append(c1);
             System.out.println(c);
        }
    }
Hello
World
Helloworld
Hello World
Explanation:
append() method of class StringBuilder is used to concatenate the string representation to the end of invoking string.
21181.What is the output of this program?
    class output {
        public static void main(String args[]){ 
             StringBuilder c = new StringBuilder("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