Home » Java programming language

Java StringBuffer reverse() method with Example

Java StringBuffer reverse() method: Here, we are going to learn about the reverse() method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on June 29, 2019

StringBuffer Class reverse()

  • This method is available in package java.lang.StringBuffer.reverse().
  • This method is used to reverse the representation of the objects.
  • This method does not raise an exception during runtime.

Syntax:

    StringBuffer reverse(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the StringBuffer.

Return value:

The return type of this method is StringBuffer that means this method returns a reference to this object.

Java program to demonstrate example of reverse() method

import java.lang.StringBuffer;

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

        StringBuffer sb = new StringBuffer("Java is a programming language");

        // use reverse() it will reverse the characters of the sb object.
        // Display result after reversing string sequence

        System.out.println("The result will be after reversing a string :" + sb.reverse());

        sb = new StringBuffer(123456);

        // use reverse() it will reverse the integers
        // Display result after inserting
        System.out.println("The result will be after reversing an string object :" + sb.reverse());
    }
}

Output

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after reversing a string :egaugnal gnimmargorp a si avaJ
The result will be after reversing an integer object :654321


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.