StringBuffer reverse() Method in Java with Examples

The Java.lang.StringBuffer.reverse() is an inbuilt method which is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence.
Syntax :

public StringBuffer reverse()

Parameters : The method does not take any parameter .

Return Value : The method returns the StringBuffer after reversing the characters.
Examples :

Input: StringBuffer = GeeksforGeeks
Output = !skeegrofskeeG

Input: StringBuffer = Hello World
Output = !dlroW olleH

Below programs illustrate the java.lang.StringBuffer.reverse() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the
// java.lang.StringBuffer.reverse()
import java.lang.*;
  
public class Test {
  
    public static void main(String args[])
    {
        StringBuffer sbf = new StringBuffer("Geeksforgeeks!");
        System.out.println("String buffer = " + sbf);
          
        // Here it reverses the string buffer
        sbf.reverse();
        System.out.println("String buffer after reversing = " + sbf);
    }
}

chevron_right


Output:

String buffer = Geeksforgeeks!
String buffer after reversing = !skeegrofskeeG

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the
// java.lang.StringBuffer.reverse()
import java.lang.*;
  
public class Test {
  
    public static void main(String args[])
    {
        StringBuffer sbf = new StringBuffer("1 2 3 4 5 6 7 8 9 10");
        System.out.println("String buffer  = " + sbf);
  
        // Here it reverses the string buffer
        sbf.reverse();
        System.out.println("String buffer after reversing = " + sbf);
    }
}

chevron_right


Output:

String buffer  = 1 2 3 4 5 6 7 8 9 10
String buffer after reversing = 01 9 8 7 6 5 4 3 2 1


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.