How to Reverse a String in Java
"Reverse a string" is often the first coding question in a fresher interview. The interviewer usually wants to see two things: that you know the built-in way, and that you can do it by hand with a loop, which shows you understand indexes. This lesson covers both, plus recursion, and explains one trap that even experienced developers miss.
The Built-In Way
public class BuiltIn {
public static void main(String[] args) {
String reversed = new StringBuilder("hello").reverse().toString();
System.out.println(reversed);
}
}ollehStrings can't change, so you copy the text into a StringBuilder, reverse it there, and turn it back into a String. This is the version to use in real code.
By Hand: Swapping from Both Ends
public class TwoPointer {
static String reverse(String s) {
char[] c = s.toCharArray();
for (int i = 0, j = c.length - 1; i < j; i++, j--) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
return new String(c);
}
public static void main(String[] args) {
System.out.println(reverse("Navika"));
System.out.println(reverse("racecar"));
System.out.println("[" + reverse("") + "]");
}
}akivaN
racecar
[]How it works
toCharArray()gives an array you can change.istarts at the first position andjat the last. Each pass swaps those two characters, then moves both pointers inward.- The loop stops when they meet (
i < jis false), so each character moves exactly once: about n/2 swaps for n characters. - A palindrome like "racecar" reads the same both ways, and an empty string stays empty.
Two More Ways
public class OtherWays {
static String backward(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
static String recursive(String s) {
if (s.isEmpty()) {
return s;
}
return recursive(s.substring(1)) + s.charAt(0);
}
public static void main(String[] args) {
System.out.println(backward("Srihitha"));
System.out.println(recursive("Advitha"));
}
}ahtihirS
ahtivdA| Way | Extra memory | Notes |
|---|---|---|
StringBuilder.reverse() | One builder | Shortest; handles emoji correctly |
| Two-pointer swap | One char[] | The classic interview answer |
| Backward loop | One builder | Easy to read |
| Recursion | A new string per level | Elegant, but slow and can overflow the stack for long text |
The Emoji Trap
Emoji take two chars in Java (see "Character Encoding and Charsets in Java"). A hand-written swap reverses those two halves too, which breaks the emoji into two invalid pieces. StringBuilder.reverse() knows about these pairs and keeps them together:
| Method | "ab😀" reversed |
|---|---|
new StringBuilder("ab😀").reverse() | 😀ba (correct) |
Two-pointer swap on toCharArray() | Two broken characters, then ba |
Common Mistakes
Building the result with += in the loop. It's slow for long strings; use a StringBuilder or a char[].
Looping with i <= j and swapping the middle character with itself. Harmless, but i < j is cleaner.
Recursion on very long text. Each character adds a stack frame and a new string.
Interview Corner
How do you reverse a string in Java?
The simplest way is new StringBuilder(s).reverse().toString(), which also keeps surrogate pairs such as emoji intact. Manually, convert the string to a char[] and swap characters from both ends with two indexes moving toward the middle, then build a new String from the array. That takes linear time and n/2 swaps.
Practice
- Easy: Reverse each word in
"Java is fun"separately, giving"avaJ si nuf". - Harder: Write
static boolean isPalindrome(String s)with the two-pointer idea, without building a reversed copy, ignoring case.
Key Takeaways
Was this tutorial helpful?