Reverse a String in Java - Multiple Ways
Learn how to reverse a string in Java using StringBuilder, character array and recursion.
Last updated: 11 January 2026
Method 1: Using StringBuilder
Using built-in reverse() method.
Main.javaRun in Compiler →
public class ReverseString {
public static void main(String[] args) {
String str = "Hello World";
// StringBuilder is mutable
StringBuilder sb = new StringBuilder(str);
sb.reverse();
System.out.println("Reversed String: " + sb.toString());
}
}Output:
Reversed String: dlroW olleH
Explanation
StringBuilder class provides a reverse() method to reverse the characters.
Method 2: Using char array
Manually reversing characters.
Main.javaRun in Compiler →
public class ReverseString {
public static void main(String[] args) {
String str = "Hello";
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length / 2; i++) {
char temp = chars[i];
chars[i] = chars[chars.length - 1 - i];
chars[chars.length - 1 - i] = temp;
}
System.out.println(new String(chars));
}
}Output:
olleH
Explanation
Convert to char array, swap start and end characters moving inward.
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler