Reverse String in Java

JavaJavaBeginner
Practice Now

Introduction

In Java, a String is a sequence of characters. In some cases, it may be necessary to reverse a String. This can be done using the reverse() method of the StringBuilder class or by creating a new string in reverse order. In this lab, we will go through the steps to reverse a string in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/StringManipulationGroup -.-> java/stringbuffer_stringbuilder("`StringBuffer/StringBuilder`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117451{{"`Reverse String in Java`"}} java/classes_objects -.-> lab-117451{{"`Reverse String in Java`"}} java/class_methods -.-> lab-117451{{"`Reverse String in Java`"}} java/modifiers -.-> lab-117451{{"`Reverse String in Java`"}} java/oop -.-> lab-117451{{"`Reverse String in Java`"}} java/identifier -.-> lab-117451{{"`Reverse String in Java`"}} java/stringbuffer_stringbuilder -.-> lab-117451{{"`Reverse String in Java`"}} java/arrays -.-> lab-117451{{"`Reverse String in Java`"}} java/comments -.-> lab-117451{{"`Reverse String in Java`"}} java/data_types -.-> lab-117451{{"`Reverse String in Java`"}} java/for_loop -.-> lab-117451{{"`Reverse String in Java`"}} java/operators -.-> lab-117451{{"`Reverse String in Java`"}} java/output -.-> lab-117451{{"`Reverse String in Java`"}} java/strings -.-> lab-117451{{"`Reverse String in Java`"}} java/variables -.-> lab-117451{{"`Reverse String in Java`"}} java/object_methods -.-> lab-117451{{"`Reverse String in Java`"}} java/string_methods -.-> lab-117451{{"`Reverse String in Java`"}} java/system_methods -.-> lab-117451{{"`Reverse String in Java`"}} end

Create a String

The first step is to create a String that we want to reverse. We will create a String variable and assign it a string value.

String str = "Studytonight";

Use StringBuilder to Reverse the String

In this step, we will use the reverse() method of the StringBuilder class to reverse the string. We will create a new StringBuilder object and pass the original string as an argument. Then, we will call the reverse() method on the StringBuilder object. Finally, we will convert the StringBuilder object back to a String using the toString() method.

StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
String reversedStr = strBuilder.toString();

We can print the reversed string using the following code.

System.out.println("Reversed String using StringBuilder: " + reversedStr);

Create a New String in Reverse Order

In this step, we will create a new string in reverse order. We will use a for loop to iterate through each character of the original string starting with the last character. We will append each character to a new String object.

String newStr = "";
for(int i = str.length() - 1; i >= 0; i--) {
   newStr = newStr + str.charAt(i);
}

We can print the reversed string using the following code.

System.out.println("Reversed String using new String: " + newStr);

Final Code

The final code to reverse the string using StringBuilder and new String is as follows.

public class ReverseString {
   public static void main(String[] args) {
      String str = "Studytonight";

      // Reversing a String using StringBuilder
      StringBuilder strBuilder = new StringBuilder(str);
      strBuilder.reverse();
      String reversedStr = strBuilder.toString();
      System.out.println("Reversed String using StringBuilder: " + reversedStr);

      // Reversing a String using new String
      String newStr = "";
      for(int i = str.length() - 1; i >= 0; i--) {
         newStr = newStr + str.charAt(i);
      }
      System.out.println("Reversed String using new String: " + newStr);
   }
}

Summary

In this lab, we learned how to reverse a string in Java using StringBuilder and new String. We also went through the steps to create a new String in reverse order using a for loop. It is important to note that the StringBuilder method is more efficient for large strings and should be used when performance is a concern.

Other Java Tutorials you may like