Reverse a string in Java

9    Asked by jacack_2023 in Java , Asked on Sep 14, 2025

How do you reverse a string in Java, and what are the different ways to achieve it? Java provides multiple approaches—using StringBuilder, loops, or even streams—to flip a string and read it backwards.

Answered by Manoj Singh

Reversing a string in Java is a common programming task, and there are several ways to achieve it depending on the situation. Since strings in Java are immutable, you can’t directly modify them, but you can use different techniques to create a reversed version.

Here are some of the most common methods:

Using StringBuilder or StringBuffer:

 The easiest and most efficient approach is to use StringBuilder’s built-in .reverse() method.

 String text = "Hello Java";
StringBuilder sb = new StringBuilder(text);
String reversed = sb.reverse().toString();
System.out.println(reversed); // Output: avaJ olleH

Using a for loop:

 You can manually reverse a string by iterating from the last character to the first.

 String text = "Hello Java";
String reversed = "";
for (int i = text.length() - 1; i >= 0; i--) {
    reversed += text.charAt(i);
}
System.out.println(reversed); // Output: avaJ olleH

Using character arrays:

 Converting the string to a char[], swapping characters, and then reconstructing it is another option.

Using Java 8 Streams:

 With streams, you can reverse using collectors and lambdas, though it’s less efficient compared to StringBuilder.

In short, the most recommended method is using StringBuilder.reverse(), as it is concise and efficient. However, understanding loop-based or array-based methods is useful for interviews and strengthening your grasp of Java fundamentals.



Your Answer

Interviews

Parent Categories