Java Tutorial
🔍

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.

How It Works
  1. 1A String in Java is immutable, so the first step is converting it to a char[] with toCharArray().
  2. 2Two pointers, left and right, start at index 0 and length - 1 — the opposite ends of the array.
  3. 3At each step, the characters at left and right are swapped using a temp variable, then left increments and right decrements.
  4. 4The loop stops once left meets or crosses right, meaning every character has been swapped exactly once.
  5. 5The mutated array is rebuilt into a String with new String(chars), giving the reversed result.
For "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']

Stepleft, rightSwapArray State
10, 4swap h ↔ oo e l l h
21, 3swap e ↔ lo l l e h
32, 2left == right → stopo 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

MetricComplexity
Time ComplexityO(n)
Space ComplexityO(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

Practice Programs

Put this topic into practice — each program has full code, output and a step-by-step explanation you can run online.

Browse all Java programs →