String reverse() Function in Java

String reverse() Function in Java | Java language provides a lot of built-in functions to simplify the programmer’s work. Similar to the String class, the StringBuilder & StringBuffer class also contains many methods to perform different methods on string type values. The reverse() method defined in the StringBuilder & StringBuffer class is used to reverse a given string value. Note:- reverse() function is not available in the String class, it is available in StringBuilder and StringBuffer classes. Also see:- How To Reverse a String In Java

The reverse function in the Java StringBuilder class is defined as,

Method prototype in StringBuilder class:- public StringBuilder reverse()
Return value:- StringBuilder

@Override
public StringBuilder reverse() {
   super.reverse();
   return this;
}

The reverse function in the Java StringBuffer class is defined as,

Method prototype in StringBuffer class:- public synchronized StringBuffer reverse()
Return value:- StringBuffer

@Override
public synchronized StringBuffer reverse() {
   toStringCache = null;
   super.reverse();
   return this;
}

Both StringBuffer and StringBuilder classes are extended from AbstractStringBuilder. All the methods of the StringBuffer class are synchronized therefore they are best suitable in a multithreading environment whereas StringBuilder is mostly used in single-thread applications. 

Program for String reverse() Function in Java

Now let us see the example of string reverse function in Java using the StringBuilder class.

public class Main {
   public static void main(String args[]) {
      String string =  "Welcome to Know Program";
      // convert string to StringBuilder
      
      StringBuilder sb = new StringBuilder(string);
      // call reverse() function
      sb.reverse();
      
      // store result to String by
      // converting StringBuilder to String
      String reverse = sb.toString();
      
      // display strings
      System.out.println("Original String: " + string);
      System.out.println("Reversed String: " + reverse);
   }
}

Output:-

Original String: Welcome to Know Program
Reversed String: margorP wonK ot emocleW

The conversion and reversing can be done in a single line as follows:-

public class Main {
   public static void main(String args[]) {
      String string = "I am learning Java programming language";
      System.out.println("Original String: " + string);

      string = new StringBuilder(string).reverse().toString();
      System.out.println("Reversed String: " + string);
   }
}

Output:-

Original String: I am learning Java programming language
Reversed String: egaugnal gnimmargorp avaJ gninrael ma I

In the above program, a new StringBuilder(string) is used to convert the string object to StringBuilder so that we can call the reverse() function. The reverse() is called on the StringBuilder object and again the toString() method is called to convert the StringBuilder value to String type value.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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