StringBuilder reverse does not produce a new StringBuilder instance. It causes the underlying characters of the current StringBuilder to be reversed. So,

String a = s.reverse().toString(); 
String b = s.toString();

The second s.toString() is operating on the reversed StringBuilder.

you have to do

String original = s.toString(); 
String reversed = s.reverse().toString();
return original.equals(reversed);
Answer from Thiyagu on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-reverse-in-java-with-examples
StringBuilder reverse() in Java with Examples - GeeksforGeeks
October 18, 2018 - The reverse method of the StringBuilder class is used to reverse the sequence of characters in a StringBuilder object.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › stringbuilder reverse method in java
StringBuilder reverse Method in Java
September 1, 2008 - The Java StringBuilder reverse() method is used to reverse the characters of a StringBuilder object. It replaces the sequence of characters in reverse order.
🌐
Codecademy
codecademy.com › docs › java › stringbuilder › .reverse()
Java | StringBuilder | .reverse() | Codecademy
August 22, 2022 - The .reverse() method returns a modified StringBuilder object with its character sequence rearranged in the opposite order. This is the most straightforward way to reverse a string in Java.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
October 20, 2025 - public StringBuilder reverse() Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
🌐
Medium
medium.com › @AlexanderObregon › javas-stringbuilder-reverse-method-explained-b2b701ee2029
Java’s StringBuilder.reverse() Method Explained | Medium
October 3, 2024 - Another common use case for StringBuilder.reverse() is reversing text streams, which can be useful in certain types of data processing. For example, you might need to reverse logs, output text in reverse order, or transform data streams in scenarios like network communication or file reading. Consider the following example where we reverse user input: import java.util.Scanner; public class ReverseStreamExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a line of text:"); String input = scanner.nextLine(); StringBuilder sb = new StringBuilder(input); System.out.println("Reversed: " + sb.reverse().toString()); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: StringBuilder objects are mutable, and their reverse() method reverses the content in-place, which is faster than manual looping. We can use character array to reverse a string.
Published   October 14, 2025
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-reverse
Java StringBuilder reverse()
It returns a StringBuilder instance that contains the reverse of the given sequence. public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Tomato"); System.out.println("Given String: "+sb); //reverse the String "Tomato" sb.reverse(); //print ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › how to reverse a string in java
How to Reverse a String in Java | Baeldung
January 8, 2024 - As shown above, we used StringBuilder.reverse() as a mapper to invert the specified string.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 6)
public StringBuilder reverse() Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
public StringBuilder reverse() Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java reverse string: tutorial with programming examples
Java Reverse String: Tutorial With Programming Examples
April 1, 2025 - Answer: No. The String class does not have a reverse() method. However, you can reverse a String using multiple ways in the String class itself. Also, StringBuilder, StringBuffer, and Collections support the reverse() method.
🌐
Java67
java67.com › 2012 › 12 › how-to-reverse-string-in-java-stringbuffer-stringbuilder.html
How to Reverse String in Java with or without StringBuffer Example | Java67
Here is my complete code program to reverse any String in Java. In the main method, we have first used StringBuffer and StringBuilder to reverse the contents of String, and then we wrote our own logic to reverse String. This uses the toCharArray() method of String class which returns the character array of String.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.stringbuilder.reverse
StringBuilder.Reverse Method (Java.Lang) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Reverses the order of characters in this builder. [Android.Runtime.Register("reverse", "()Ljava/lang/StringBuilder;", "")] public Java.Lang.St...
Top answer
1 of 2
1
The previous solution given will work! I'm going to suggest some modifications. Here's the original:public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.substring(i, i + 1); } return reversed;}That call to str.substring(i, i + 1) is essentially a way of picking out a single character at index i, but there's actually a built in String method for that: charAt. And I think things read a bit more easily using it. Using it is also a bit semantically clearer in my opinion (others can disagree!):public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } return reversed;}It's generally better to use a StringBuilder, though, since that keeps us from generating a lot of Strings along the way (what if our input String was a very long String)?public static String reverse(String str) { StringBuilder reversed = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { reversed.insert(0, str.charAt(i)); } return reversed.toString();}Note that here, I actually iterated forward through the String and prepended (using insert), but you could iterate backwards and use the append method, too. But the main point is that using a StringBuilder keeps us from having to generate new Strings for each character in the input.I hope that helps!
2 of 2
0
We can't use a pre-defined function in java that can instantaneously reverse a string. Therefore, we must come up with a way to do so. One way we can do this is by creating a new string adding individual characters to it by traversing through the original string in reverse order. This will result in the final string being the reverse of the original string, and this will be our solution to the problem. An implementation of this solution looks like:public class MyClass {public static void main(String args[]) {String before="hello";String after=reverse(before);System.out.println(after);}public static String reverse(String str){String reversed="";for(int i=str.length()-1; i>=0; i--){reversed+=str.substring(i,i+1);}return reversed;}}
Top answer
1 of 3
4

I think you are on the right lines, just a few things I'd change: Firstly we have the ability to specify the length of the StringBuilder because we know it is going to be the same length as the String we are reversing.

It is not necessary to loop through the entire array, just half the array will suffice performing operations which are essentially just switching the characters. So the right-most character is now the left-most character, this saves a little computation.

public static void main(String[] args) throws IOException {
    String str = "Reverse me";
    StringBuilder printStr = new StringBuilder();
    printStr.setLength(str.length());

    for(int i =0; i<str.length()/2;i++){
        char left = str.charAt(i);
        final int fromRight = str.length() - i - 1;
        printStr.insert(i, str.charAt(fromRight));
        printStr.insert(fromRight, left);
    }
}

Here is a visual representation of how to character switching essentially works: More information can be found here

2 of 3
1

OK, so you made multiple mistakes, you are trying to access indexes by using parenthesis, you are not iterating over whole string as your loop condition is i > 0 and it should be i >= 0, and your loop is just wrong (disregard parenthesis), you can combine append() and charAt() methods to do what you need:

    for (int i = str.length()-1; i >= 0; i--) {
        printStr.append(str.charAt(i));
    }

We go from last index of your string up to index 0, so we start from the end of the string, and we append each character to our StringBuilder. In the end you have your reversed string.

🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › stringbuilder › reverse() method example
Java StringBuilder reverse() method example
September 30, 2019 - Basically the reverse() method causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation.
🌐
Java Guides
javaguides.net › 2024 › 06 › java-stringbuilder-reverse-method.html
Java StringBuilder reverse() Method
June 10, 2024 - The StringBuilder.reverse() method in Java is used to reverse the sequence of characters in a StringBuilder object.
🌐
W3Schools Blog
w3schools.blog › home › java stringbuilder reverse() method
Java StringBuilder reverse() method - W3schools
August 28, 2014 - System.out.println(sb.reverse()); } } public class StringBuilderReverseExample { public static void main(String args[]){ //creating TestStringBuilder object TestStringBuilder obj = new TestStringBuilder(); //method call obj.reverseTest(); } } ... Error:java: Source option 5 is no longer supported.