Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - StringBuffer


Once a String type object is created, it cannot be changed or modified.


And this is where StringBuffer comes handy.


Java StringBuffer class is used to create String objects that can be changed or modified.


And the StringBuffer class is thread safe.


We will seeing two ways by which we can use the java StringBuffer class.


First way of creating a StringBuffer object


Example :



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

        StringBuffer x = new StringBuffer("Hello");
        x.append("World");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloWorld

So, in the above example, we have created a StringBuffer object and initialised Hello to it.


StringBuffer x = new StringBuffer("Hello");
Spring_Boot


In the next line, we are going to club World with Hello and make it HelloWorld.


To achieve that we would be using the append() method provided by StringBuffer.


x.append("World");

So, the append() method adds the String World with Hello and we get the String HelloWorld.

Spring_Boot

In the next line, we have created a String x and initialised it with the StringBuffer object after converting the StringBuffer object to String.


String s = x.toString();

And if we see the output, we can see that HelloWorld is printed on the screen.

Output :



  HelloWorld

Now, let us see the second way using which we can create a StringBuffer object.


Second way of creating a StringBuffer object


Example :



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

        StringBuffer x = new StringBuffer();
        x.append("Hello");
        x.append("World");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloWorld

In the above example, we have created an empty StringBuffer object,


StringBuffer x = new StringBuffer();
Spring_Boot


Now, in the next line, we have used the append() method of StringBuffer to insert Hello to the StringBuffer object,


x.append("Hello");
Spring_Boot


Then in the next line, we have used the append() method again to add the String World after Hello and make it HelloWorld.


x.append("World");
Spring_Boot


Then we are using the toString() method of StringBuffer to convert the StringBuffer object to a String object.


String s = x.toString();

And assign it to a String type variable x.

Spring_Boot

Now, if we see the output, HelloWorld is printed on the screen.

Output :



  HelloBeautifulWorld

Other ways of using StringBuffer class


Using the insert() method of StringBuffer class


Say, we have a String HelloWorld and we want to insert a new String called Beautiful and make the new String HelloBeautifulWorld


Example :



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

        StringBuffer x = new StringBuffer("HelloWorld");

        x.insert(5, "Beautiful");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloBeautifulWorld

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuffer object.


StringBuffer x = new StringBuffer("HelloWorld");

Now, we want to insert a new String called Beautiful inside Hello and World.


In other words, we have to insert the String Beautiful in 5th index position (i.e. Right after the String Hello).


x.insert(5, "Beautiful");

And if we see the output, HelloBeautifulWorld is printed on the screen.

Output :



  HelloBeautifulWorld

Using the replace() method of StringBuffer class


Say, we have a String HelloWorld and we want to replace the subString Wor with Beautiful and make the new String HelloBeautifulld.


Example :



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

        StringBuffer x = new StringBuffer("HelloWorld");

        x.replace(5, 8, "Beautiful");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloBeautifulld

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuffer object.


StringBuffer x = new StringBuffer("HelloWorld");

In the next line, we are going to replace the subString Wor with the String Beautiful.


x.replace(5, 8, "Beautiful");

And if we see the output, HelloBeautifulld is printed on the screen.

Output :



  HelloBeautifulld

Using the delete() method of StringBuffer class


Say, we have a String HelloWorld and we want to delete the subString World and keep the new String Hello.


Example :



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

        StringBuffer x = new StringBuffer("HelloWorld");

        x.delete(5,10);

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  Hello

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuffer object.


StringBuffer x = new StringBuffer("HelloWorld");

In the next line, we are going to delete the subString World from the String HelloWorld.


x.delete(5,10);

And if we see the output, Hello is printed on the screen.

Output :



  Hello

Using the reverse() method of StringBuffer class


Say, we have a String HelloWorld and we want to reverse the String HelloWorld and make it dlroWolleH.


Example :



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

        StringBuffer x = new StringBuffer("HelloWorld");

        x.reverse();

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  dlroWolleH

The above code is self explanatory.


We have used the reverse() method of the StringBuffer class to reverse the String HelloWorld and make it dlroWolleH.


x.reverse();

And we got the below reversed String as output.