Java StringBuffer.reverse() method explanation with examples

Java StringBuffer.reverse() method explanation with examples:

The StringBuffer.reverse method can be used to reverse the content or characters of a StringBuffer in Java. Unlink strings, StringBuffer is mutable. So, this method replaces the content of the StringBuffer on which we are calling the reverse() method.

In this post, we will learn the definition of StringBuffer.reverse() and its use with different examples.

Definition of StringBuffer.reverse() method:

The reverse() method is a public method and it is defined in the StringBuffer class. It is defined as like below:

public StringBuffer reverse()

It creates the reverse of the current sequence and replaces the current sequence by the reverse sequence. For any surrogate pairs, it treats it as single characters while doing the reverse.

Return of StringBuffer.reverse() method:

It returns a reference to the current object of the StringBuffer.

Example program of StringBuffer.reverse():

Let’s take an example of StringBuffer.reverse():

class Main{
    public static void main(String[] args){
        StringBuffer stringBuffer = new StringBuffer("Hello World !!");

        System.out.println("Given StringBuffer: "+stringBuffer);

        stringBuffer.reverse();

        System.out.println("StringBuffer after reverse() is called: "+stringBuffer);
    }
}

It will give output as like below:

Given StringBuffer: Hello World !!
StringBuffer after reverse() is called: !! dlroW olleH

As you can see here,

  • stringBuffer is the initial StringBuffer variable.
  • We are calling reverse() method on this variable and printing its value before and after reverse() is called.
  • It replaces the content of the original StringBuffer variable.

The reverse() method returns the reference to the same StringBuffer variable. So, if I change the above program as like below:

class Main{
    public static void main(String[] args){
        StringBuffer stringBuffer = new StringBuffer("Hello World !!");

        System.out.println("Given StringBuffer: "+stringBuffer);
        System.out.println("StringBuffer after reverse() is called: "+stringBuffer.reverse());
    }
}

It will print the same output.

How to reverse a user-input string by using StringBuffer.reverse():

We can reverse a user-input string by converting it to a StringBuffer. Strings are immutable in Java. We can’t change a string directly.

If we have to reverse a string, we have to create one mutable variable with its value. We can pass a string to the constructor of StringBuffer to get a StringBuffer variable. Once we create a StringBuffer variable, we can call reverse() method on it to reverse its content. We can call toString() method on this variable to convert it back to a string.

The below program takes a string as an input from the user and reverses that string.

import java.util.Scanner;

class Main{
    public static void main(String[] args){
        String str;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string: ");
        str = sc.nextLine();
        System.out.println("Given string: "+str);

        StringBuffer stringBuffer = new StringBuffer(str);
        stringBuffer.reverse();
        str = stringBuffer.toString();

        System.out.println("Reversed string: "+str);
    }
}

Here,

  • str is a string variable. It is taking a string as the input from the user and storing that value in the str variable.
  • We are creating a StringBuffer object by passing the string value as the parameter. The created object is stored in the stringBuffer variable.
  • We are calling reverse() method on this StringBuffer variable to reverse its content. The reverse content is stored in the stringBuffer variable.
  • Once reverse() is called, we are calling the toString() method to convert it to string. The new created string reference is stored in the str variable.
  • The last line is printing the reversed string.

If you run this program, it will print outputs as like below:

Enter a string: 
hello world
Given string: hello world
Reversed string: dlrow olleh

Java StringBuffer reverse() method

You might also like: