Java Program to Reverse a String Using reverse() method

In this program, we will learn to code the Java Program to Reverse a String Using reverse() method. Let’s understand How the reverse() method works in Java Programming Language. In previous programs, we have learned to code the Java Program to Reverse a Number.

In Java, there is no reverse() method in String Class, so we cannot reverse a string using the reverse() method directly in Java Programming Language. There are Two Classes that have the reverse() method, these are as follows,

  1. StringBuilder Class
  2. StringBuffer Class

So, using these above-mentioned classes we are going to code the Java Program to Reverse a String Using reverse() method. Let’s briefly learn about StringBuffer and StringBuilder Class.

What is StringBuilder?

In Java, the StringBuilder Class represents a mutable Sequence of characters unlike String Class as we all know that Strings in Java are immutable.

The StringBuilder Class has the reverse() method using which we are going to reverse the String. We will convert the given String to StringBuilder then by using the reverse() method we will reverse the String.

Let’s see the code to Java Program to Reverse a String Using reverse() method using StringBuilder.

Java Program to Reverse a String Using reverse() method

import java.util.*;
public class Main
{
    public static void main(String arr[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();
        
        StringBuilder sb = new StringBuilder(str);
        String revString = sb.reverse().toString();
        
        System.out.println("Reversed String : "+revString);
    }
}

Output 1

Enter a String: java
Reversed String : avaj

Output 2

Enter a String: codingbroz
Reversed String : zorbgnidoc

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();

In this program, we take the String as input from the user using Scanner class next() method in Java and store it in variable str of String type.

        StringBuilder sb = new StringBuilder(str);

Then, we created an object of the StringBuilder class by passing the string as an argument in the constructor and an object sb is created.

        String revString = sb.reverse().toString();

Then, using the reverse() method of StringBuilder class we reversed the StringBuilder object and then using the toString() method we converted it into String.

        System.out.println("Reversed String : "+revString);

Then, we display the result using the System.out.println() function.

This is the Java Program to Reverse a String Using reverse() method using the StringBuilder Class. Now, let’s see the same using StringBuffer Class.

What is StringBuffer?

The StringBuffer class is the same as the StringBuilder Class which is used to represent a mutable Sequence of characters. The main difference between StringBuilder and StringBuffer is that StringBuffer Class is synchronized and StringBuilder is not.

Let’s see the code to Java Program to Reverse a String Using reverse() method using StringBuffer.

Java Program to Reverse a String Using reverse() method

import java.util.*;
public class Main
{
    public static void main(String arr[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();
        
        StringBuffer sb = new StringBuffer(str);
        String revString = sb.reverse().toString();
        
        System.out.println("Reversed String : "+revString);
    }
}

Output 1

Enter a String: java
Reversed String : avaj

Output 2

Enter a String: codingbroz
Reversed String : zorbgnidoc

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();

In this program, we take the String as input from the user using Scanner class next() method in Java and store it in variable str of String type.

  StringBuffer sb = new StringBuffer(str);

Then, we created an object of the StringBuffer class by passing the string as an argument in the constructor and an object sb is created.

        String revString = sb.reverse().toString();

Then, using the reverse() method of StringBuffer class we reversed the StringBuffer object and then using the toString() method we converted it into String.

        System.out.println("Reversed String : "+revString);

Then, we display the result using the System.out.println() function.

This is the Java Program to Reverse a String Using reverse() method using the StringBuffer Class.

Conclusion

I hope after going through this post, you understand the Java Program to Reverse a String Using reverse() method. We discussed the program using both classes i.e. StringBuilder and StringBuffer. If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *