Java StringBuffer reverse() Method

The StringBuffer.reverse() method in Java is used to reverse the sequence of characters in a StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how StringBuffer.reverse() can be used effectively.

Table of Contents

  1. Introduction
  2. reverse Method Syntax
  3. Examples
    • Reversing a StringBuffer
    • Reversing a Palindrome
  4. Real-World Use Case
    • Example: Creating a Mirror Image of a String
  5. Conclusion

Introduction

The reverse() method is a member of the StringBuffer class in Java. It reverses the sequence of characters in the StringBuffer, which can be useful for various string manipulation tasks.

reverse Method Syntax

The syntax for the reverse method is as follows:

public synchronized StringBuffer reverse()
  • Parameters: None
  • Returns: A reference to the same StringBuffer object, with the characters in reverse order.

Examples

Reversing a StringBuffer

The reverse method can be used to reverse the characters in a StringBuffer object.

Example

public class ReverseExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello, world!");

        // Reversing the StringBuffer
        sb.reverse();

        // Printing the reversed StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

!dlrow ,olleH

Reversing a Palindrome

Reversing a palindrome should result in the same string, demonstrating the nature of palindromes.

Example

public class PalindromeExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("madam");

        // Reversing the StringBuffer
        sb.reverse();

        // Printing the reversed StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

madam

Real-World Use Case

Example: Creating a Mirror Image of a String

A common real-world use case for StringBuffer.reverse() is creating a mirror image of a string.

Example

public class MirrorImageExample {
    public static void main(String[] args) {
        StringBuffer text = new StringBuffer("12345");

        // Creating a mirror image by appending the reverse
        StringBuffer mirrorImage = new StringBuffer(text).append(new StringBuffer(text).reverse());

        // Printing the mirror image
        System.out.println(mirrorImage.toString());
    }
}

Output:

1234554321

In this example, StringBuffer.reverse() is used to create a mirror image of the string "12345" by appending its reverse to the original string.

Conclusion

The StringBuffer.reverse() method in Java provides a way to reverse the sequence of characters in a StringBuffer object. By understanding how to use this method, you can efficiently manipulate strings in your Java applications. The method allows you to perform various string reversal operations, making it a versatile tool for string manipulation in various scenarios.

Comments