VOOZH about

URL: https://www.geeksforgeeks.org/java/reverse-a-string-in-java/

⇱ Reverse a String in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reverse a String in Java

Last Updated : 12 May, 2026

A string reversal means changing the order of characters from beginning to end in the opposite direction. In Java, reversing a string is a popular programming exercise that helps beginners understand loops, string manipulation, and built-in methods.

Example:

Input String -> Java Programming
Reversed String -> gnimmargorP avaJ

Ways to Reverse a String

Java provides multiple ways to reverse a string efficiently.

1. Using a For Loop

The for loop is the most basic and manual approach. It provides complete control over the reversal process without using additional classes.


Output
skeeG

Explanation: Each character of the string is appended in reverse order to a new string r, resulting in the reversed output.

2. Using StringBuilder.reverse()

StringBuilder provides a built-in reverse() method, making string reversal quick and efficient.


Output
skeeG

Explanation: StringBuilder objects are mutable, and their reverse() method reverses the content in-place, which is faster than manual looping.

3. Using Character Array

We can use character array to reverse a string. Follow Steps mentioned below:

  • First, convert String to character array by using the built-in Java String class method toCharArray().
  • Then, scan the string from end to start, and print the character one by one.

Output
skeeG

Explanation: The string is converted into a char[] array, and characters are printed in reverse order using a simple loop.

4. Using Collections.reverse()

The Collections.reverse() method can be used when dealing with lists. We can convert a string to a list of characters, reverse it, and print it back.


Output
skeeG

Explanation: Characters are stored in a list and reversed using Collections.reverse(). This approach is helpful when you’re already working with Java collections.

5. Using StringBuffer.reverse()

StringBuffer is similar to StringBuilder but thread-safe. It also provides the reverse() method.


Output
skeeG

Explanation: StringBuffer is synchronized, so it’s a good choice for multithreaded environments.

6. Using a Stack

The stack approach uses the Last-In-First-Out (LIFO) principle to reverse characters.


Output
skeeG

Explanation: Each character is pushed onto the stack and then popped out, reversing the order naturally.

7. Using getBytes()

This method works at the byte level and is useful for encoding or low-level string manipulation.


Output
skeeG

Explanation: The string is converted to bytes, reversed, and then reconstructed into a new String.

When to Use Which Method

  • For Loop: For simplicity and complete manual control over the reversal process.
  • StringBuilder/StringBuffer: Efficient and concise with built-in reverse() methods.
  • Character Array: When working with individual characters or for manual control.
  • Collections.reverse(): When already working with lists or collections.
  • Stack: When following the LIFO principle or using stacks in the algorithm.
  • getBytes(): When dealing with byte-level manipulations or encoding.


Comment