Java StringBuilder reverse() Method

The StringBuilder.reverse() method in Java is used to reverse the sequence of characters in a StringBuilder 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 StringBuilder.reverse() can be used effectively.

Table of Contents

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

Introduction

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

reverse Method Syntax

The syntax for the reverse method is as follows:

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

Examples

Reversing a StringBuilder

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

Example

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

        // Reversing the StringBuilder
        sb.reverse();

        // Printing the reversed StringBuilder
        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) {
        StringBuilder sb = new StringBuilder("madam");

        // Reversing the StringBuilder
        sb.reverse();

        // Printing the reversed StringBuilder
        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 StringBuilder.reverse() is creating a mirror image of a string.

Example

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

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

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

Output:

1234554321

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

Conclusion

The StringBuilder.reverse() method in Java provides a way to reverse the sequence of characters in a StringBuilder 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