Reverse a String in Java Using Two Pointers
Program Overview
Reversing a string means printing its characters in the opposite order — "hello" becomes "olleh". It looks trivial, but it's one of the most common warm-up questions in Java interviews because it checks whether you understand character arrays, indexing, and in-place mutation instead of just calling a library method.
You'll also hit this logic in real work — generating palindrome checks, formatting reversed IDs, or processing text streams back-to-front.
Program
public class StringReverse {
public static void main(String[] args) {
String input = "hello";
System.out.println(reverse(input));
}
static String reverse(String str) {
char[] chars = str.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}Core Logic
This program reverses a string using a two-pointer swap on a char array — one pointer starts at each end, and they swap characters while moving toward the middle until the array is fully mirrored.
- 1A
Stringin Java is immutable, so the first step is converting it to achar[]withtoCharArray(). - 2Two pointers,
leftandright, start at index0andlength - 1— the opposite ends of the array. - 3At each step, the characters at
leftandrightare swapped using a temp variable, thenleftincrements andrightdecrements. - 4The loop stops once
leftmeets or crossesright, meaning every character has been swapped exactly once. - 5The mutated array is rebuilt into a
Stringwithnew String(chars), giving the reversed result.
"hello", the pointers swap 'h'↔'o' then 'e'↔'l', stopping once left meets right at the middle — producing "olleh".Key Point: This runs in O(n) time with a single pass; because Java strings are immutable, the O(n) extra space for the char array is unavoidable in a manual, built-in-free solution.
Dry Run
Input: "hello" → array ['h','e','l','l','o']
| Step | left, right | Swap | Array State |
|---|---|---|---|
| 1 | 0, 4 | swap h ↔ o | o e l l h |
| 2 | 1, 3 | swap e ↔ l | o l l e h |
| 3 | 2, 2 | left == right → stop | o l l e h |
Output
Output:
olleh
Optimized Approach
The two-pointer swap above is already the efficient version — same time complexity as any other correct approach, but no extra array or recursion overhead. In production code (not an interview), Java's built-in reverse is simpler to reach for:
String reversed = new StringBuilder("hello").reverse().toString();Use the manual version when an interviewer asks you to reverse a string without built-in methods — that's almost always the actual question being tested.
Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(n) |
One pass through the string to swap all characters — O(n) time. Space is O(n) because Java strings are immutable, so a new char[] (and then a new String) has to be created; true in-place mutation of the original string isn't possible.
Interview Tip
Q: How would you reverse a string without using StringBuilder or any built-in reverse method?
A: Convert the string to a char[], then swap characters from both ends inward using two pointers until they meet — the exact approach shown above. Mentioning why (String is immutable in Java) is what separates a strong answer from someone who just memorized the code.
What to Read Next
Learn the built-in methods you'd reach for outside an interview setting.
Practice Programs
Put this topic into practice — each program has full code, output and a step-by-step explanation you can run online.
Reverse a string using a loop.
Reverses the order of words in a sentence.
Reverses the letters within each word, keeping the word order intact.
Check whether a string reads the same forwards and backwards.
Checks whether a string is a palindrome by recursively narrowing the comparison window from both ends.