Effortless String Reverse Java Example Code Tutorial

Robert Jul 13, 2026

In the realm of software development, string manipulation is an essential skill. One common operation is reversing a string, which could be useful for various purposes such as palindrome detection or string formatting. Java, being a popular programming language, provides several ways to reverse a string. Let's explore a few examples and understand the concepts behind them.

How to Reverse a String in place in Java - Example
How to Reverse a String in place in Java - Example

Before we dive in, let's prepare an example string to reverse. We'll use "java persistence" for our demonstrations, as it's a relevant term for Java developers and has a good mix of characters.

[Solved] How to reverse a String in place in Java? Example
[Solved] How to reverse a String in place in Java? Example

Using StringBuilder

StringBuilder is a mutable sequence of characters, which makes it highly efficient for string manipulation. To reverse a string using StringBuilder, we can use the 'reverse' method.

Reverse a String Program in Java
Reverse a String Program in Java

Example:
    String str = "java persistence";
    StringBuilder sb = new StringBuilder(str);
    String reversed = sb.reverse().toString();

StringBuilder Reverse Method

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

In this example, we first create a StringBuilder object 'sb' from our original string. We then call the 'reverse' method on 'sb', which reverses the order of characters in the sequence. Finally, we use 'toString' to convert the StringBuilder back to a String, and store it in 'reversed'.

The output of this code would be " applicabilityertnisipsavaj".

StringBuilder with Loops

Java  Program to reverse every word in a String
Java Program to reverse every word in a String

Alternatively, you can manually reverse a string using StringBuilder and a loop. This method gives you more control over the process and can be useful when you need to perform additional operations along with the reverse.

Example:
    String str = "java persistence";
    StringBuilder sb = new StringBuilder(str);
    for (int i = sb.length() - 1; i >= 0; i--) {
         sb.append(sb.charAt(i));
    }
    String reversed = sb.toString();

In this case, we initialize our StringBuilder and then use a 'for' loop to iterate through our string in reverse order. For each character, we use 'append' method to add it to the end of our StringBuilder. The final 'toString' call gives us our reversed string.

Stop Struggling with String Manipulation
Stop Struggling with String Manipulation

Using String and char[]

Another way to reverse a string in Java is by using a char array. This method allows for straightforward manipulation and can be beneficial when you need to work with individual characters.

How to Reverse words in String Java? [Solution]
How to Reverse words in String Java? [Solution]
Java script string method
Java script string method
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
How to reverse a string in JavaScript
How to reverse a string in JavaScript
How to Reverse an Integer in Java without converting to String? Example and Solution
How to Reverse an Integer in Java without converting to String? Example and Solution
How to reverse a linked list in Java using Recursion and Iteration (Loop) - Example
How to reverse a linked list in Java using Recursion and Iteration (Loop) - Example
How to Reverse a String using VB.Net? With Free Source Code
How to Reverse a String using VB.Net? With Free Source Code
C code to reverse a string without strrev() Function
C code to reverse a string without strrev() Function
How to Reverse an Array in Java? Integer and String Array Example Tutorial
How to Reverse an Array in Java? Integer and String Array Example Tutorial
Java String Methods
Java String Methods
How to Reverse A String in Python?
How to Reverse A String in Python?
How to Reverse Numbers in Java, C, C#?
How to Reverse Numbers in Java, C, C#?
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Strings in Java Explained | Java Programming Basics for Beginners
Strings in Java Explained | Java Programming Basics for Beginners
String
String
JavaScript String Methods
JavaScript String Methods

Example:
    String str = "java persistence";
    char[] chars = str.toCharArray();
    StringBuilder sb = new StringBuilder(chars.length);
    for (char c : chars) {
        sb.append(c);
    }
    String reversed = sb.toString();

Here, we first convert our string to a char array using 'toCharArray'. We then create a new StringBuilder of the same length. Using a 'for-each' loop, we iterate through the char array and append each character to our StringBuilder. Finally, 'toString' gives us our reversed string.

char array with StringBuilder

In a variation of the previous example, we can also reverse the char array directly using the StringBuilder 'reverse' method. This approach is more concise but requires understanding the underlying concepts of StringBuilder and char arrays.

Example:
    String str = "java persistence";
    StringBuilder sb = new StringBuilder(str);
    char[] chars = sb.toString().toCharArray();
    sb.reverse();
    String reversed = sb.toString();

In this case, we convert our string to a character array, reverse the StringBuilder, and then convert it back to a string. The output will be the same as the previous examples, " applicabilityertnisipsavaj".

In the realm of string manipulation, understanding the 'reverse' operation is fundamental. Mastering different methods to reverse a string can help you become a more proficient Java developer. Whether you're working with StringBuilder, char arrays, or other methods, always choose the one that best fits your needs and context. Happy coding!