Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 54

Reverse a String in Java

In this lesson: Reverse a string in Java using loop, StringBuilder and char array — code, dry run and output. In Hindi & English.

Reversing a string

Reversing a string means writing its characters back to front — "Java" becomes "avaJ". Because Java strings are immutable, you always build a new reversed string. We will do it with a loop (to learn the logic) and with StringBuilder (the easy way).

💡 Strings are immutable, so you build a new reversed string, not change the original.

Reversing with a loop

public class Reverse {
    public static void main(String[] args) {
        String str = "Java";
        String rev = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            rev += str.charAt(i);    // add chars from the end
        }
        System.out.println(rev);
    }
}
avaJ

We start at the last index (length() - 1) and move down to 0, reading each character with charAt(i) and adding it to rev. Reading back-to-front gives the reverse.

Dry run

Dry Run — reversing "Java" (from the end forward)
istr.charAt(i)rev += char
3a"a"
2v"av"
1a"ava"
0J"avaJ"
Loop stops at i<0. Reverse = "avaJ"
💡 Understand it this way

The loop reads characters from the last index down to 0 and adds each to rev. So "Java" is read as a, v, a, J — building "avaJ".

Using StringBuilder

String str = "Java";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(rev);
avaJ

StringBuilder has a built-in reverse() method. This one line is the simplest and most efficient way in real code — no loop needed.

Which method to use

  • StringBuilder.reverse() — best for real code: short and fast.
  • Loop with charAt — best for learning how reversal works.

Both produce the same result. Use the loop to understand, and StringBuilder in practice.

Common mistakes

  • Starting the loop at length(). The last index is length() - 1.
  • Trying to change the string in place. Strings are immutable — build a new one.
  • Looping upward. To reverse, go from the last index down to 0.
  • Slow += for very long strings. Use StringBuilder for efficiency.

Summary

  • Reversing writes a string's characters back to front.
  • Strings are immutable, so you build a new reversed string.
  • A loop reads from length()-1 down to 0 with charAt.
  • new StringBuilder(str).reverse().toString() is the easy way.
  • Use the loop to learn, StringBuilder in real code.

Next, use this in a palindrome check, or learn text tools in String vs StringBuilder.

Frequently Asked Questions

How do I reverse a string in Java?
The easiest way is new StringBuilder(str).reverse().toString(). To understand the logic, you can also loop from the last character to the first, building a new string. Both give the reversed text.
Why can I not reverse a String in place?
Strings in Java are immutable — they cannot be changed after creation. So you always build a new reversed string rather than modifying the original. StringBuilder is mutable, which is why its reverse() works.
How does the loop method reverse a string?
You start at the last index and move to the first, appending each character to a new string. Reading the characters back to front and joining them produces the reverse.
What is the fastest way to reverse a string?
StringBuilder's built-in reverse() method is the simplest and efficient choice for real code. The manual loop is mainly for learning how reversal works.
How do I get a character at a position?
Use str.charAt(i), where i is the index starting from 0. The last character is at str.length() - 1. This is how the loop reads each character.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMatrix Multiplication Your progress: 54 / 61 (89%) · Lesson 55 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free