Monday, June 15, 2026
HomeLanguagesJavaStringBuffer reverse() Method in Java with Examples

StringBuffer reverse() Method in Java with Examples

The Java.lang.StringBuffer.reverse() is an inbuilt method that 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: NA

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

Examples:  

Input: StringBuffer = Lazyroar!
Output = !skeegrofskeeG

Input: StringBuffer = Hello World!
Output = !dlroW olleH

Below are programs that illustrate the java.lang.StringBuffer.reverse() method: 

Program 1:  

java




// Java program 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);
    }
}


Output: 

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

 

Program 2: 

java




// Java program 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);
    }
}


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

 

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS