Java Tutorial
🔍
Java ProgramsStringsReverse a String

Reverse a String in Java

beginner·  Strings  ·  String Manipulation

Problem

Reversing a string means arranging its characters in the opposite order.

Given a string, reverse it and display the result.

Input
Hello Java
Output
avaJ olleH

Java Program

Java
public class Main { public static void main(String[] args) { String str = "Hello Java"; String reversed = ""; // Walk backward from the last index to the first, appending each character for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } System.out.println(reversed); } }

Output

avaJ olleH

Core Logic

Walking backward through the string one character at a time and building up a new string as you go is the most direct way to reverse it.

How It Works
  1. 1Start at the last index — length() - 1 — since string indices run from 0 to length-1.
  2. 2Loop backward, decrementing i by one on each pass.
  3. 3At each step, charAt(i) reads the character and appends it to reversed.
  4. 4The loop stops once i drops below 0, having visited every character exactly once.
  5. 5reversed now holds the string with its characters in the opposite order.
For "Hello Java", the loop appends 'a', 'v', 'a', 'J', ' ', 'o', 'l', 'l', 'e', 'H' in turn — producing "avaJ olleH".
💡

Key Point: This approach runs in O(n) time; for very long strings, StringBuilder.reverse() avoids repeated string concatenation and performs better.

Key Concepts

Stringlength()charAt()for loop

Approach 2: StringBuilder.reverse()

Java
public class ReverseStringBuilder { public static void main(String[] args) { String str = "Hello Java"; // reverse() flips the buffer's characters in place; toString() converts it back String reversed = new StringBuilder(str).reverse().toString(); System.out.println(reversed); } }

Output

avaJ olleH

Core Logic

Skip the manual loop altogether — StringBuilder already has a reverse() method built in, and it's what you'd actually reach for in real code.

How It Works
  1. 1new StringBuilder(str) wraps the string in a mutable character buffer.
  2. 2.reverse() flips the buffer's character order in place.
  3. 3.toString() converts the reversed buffer back into a regular String.
For "Hello Java", new StringBuilder("Hello Java").reverse().toString() produces "avaJ olleH" in a single expression.
💡

Key Point: StringBuilder.reverse() is implemented internally with an in-place array swap, so it's both shorter to write and faster than repeated string concatenation.

Key Concepts

StringBuilderreverse()method chaining

Approach 3: Recursion

Java
public class ReverseStringRecursive { static String reverse(String str) { // Base case: an empty string is its own reverse if (str.isEmpty()) return str; // Move the first character to the end of the reversed remainder return reverse(str.substring(1)) + str.charAt(0); } public static void main(String[] args) { String str = "Hello Java"; System.out.println(reverse(str)); } }

Output

avaJ olleH

Core Logic

Recursion offers a different angle: peel off the first character, reverse everything after it, then tack that first character onto the end.

How It Works
  1. 1The base case if (str.isEmpty()) return str; stops the recursion once there are no characters left.
  2. 2Every other call splits the string into its first character, str.charAt(0), and the rest, str.substring(1).
  3. 3It returns reverse(str.substring(1)) + str.charAt(0) — the reversed remainder, with the first character moved to the end.
  4. 4As the recursion unwinds, each character gets appended in the opposite order it was removed.
reverse("Hi") becomes reverse("i") + 'H', and reverse("i") becomes reverse("") + 'i'"i", so the final result is "i" + "H" = "iH".
💡

Key Point: This is elegant but not efficient — substring() and string concatenation each allocate a new String, making it O(n²) overall, unlike the O(n) loop or StringBuilder versions.

Key Concepts

recursionsubstring()base case

Common Mistakes

  • Using str += str.charAt(i) inside a loop is fine for short strings, but for large inputs it's slow — each += creates a brand-new String, since strings are immutable. StringBuilder avoids that.
  • Forgetting the loop bound is str.length() - 1, not str.length() — the last valid index is always one less than the length.

Related Programs