---
title: "How to reverse a String in Java?"  
description: "How to reverse a String in Java?"  
author: "Mukul Goenka"  
published: 2021-11-16  
updated: 2023-04-28  
canonical: https://www.mindstick.com/forum/156842/how-to-reverse-a-string-in-java  
category: "java"  
tags: ["java", "programming language"]  
reading_time: 2 minutes  

---

# How to reverse a String in Java?

How to [reverse](https://www.mindstick.com/blog/63740/what-are-the-most-effective-and-safest-thanks-to-reverse-erectile-dysfunction) a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

In Java, you can reverse a string by converting it into a **StringBuilder** object, then using the **reverse()** method to reverse the characters in the string. Here's an example code snippet that demonstrates how to reverse a string in Java:

```java
public class ReverseString {
   public static void main(String[] args) {
       String originalString = "hello world";
       StringBuilder reversedString = new StringBuilder(originalString).reverse();
       System.out.println("Original string: " + originalString);
       System.out.println("Reversed string: " + reversedString);
   }
}
```

In this example, we first create a string called **originalString** that we want to reverse. We then create a **StringBuilder** object and pass the original string as a parameter to the constructor.

Next, we call the **reverse()** method on the **StringBuilder** object to reverse the characters in the string. The reversed string is then stored in the **reversedString** variable.

Finally, we print out both the original string and the reversed string using the **println()** method.

### Reply by Krishnapriya Rajeev

The code given below demonstrates how to reverse a string.

```plaintext
class reverse
{
    public static void main(String args[])
    {
        String word = "Welcome to MindStick Forum.";	// Declare and initialise a string
        int n = word.length();	// Store length of the string
        String rev = "";
        for(int i = n-1; i >= 0; i--)
        {
            rev += word.charAt(i);	// Stores the array in reverse order
        }
        System.out.println("The reverse is " + rev);
    }

}
OUTPUT:
The reverse is .muroF kcitSdniM ot emocleW
```

### Reply by Mukul Goenka

```
import java.util.Scanner;
public class HelloWorld {
 public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println('Enter any string for reverse: ');
  String str = sc.nextLine();
  System.out.println(reverse(str));
 }
 public static String reverse(String in) {
  if (in == null)
   throw new IllegalArgumentException('Null is not valid input');
  StringBuilder out = new StringBuilder();
  char[] chars = in.toCharArray();
  for (int i = chars.length - 1; i >= 0; i--)
   out.append(chars[i]);
  return out.toString();
 }
}
```

Output.

Enter any string for reverse: Here are mindstick software pvt ltd.

.dtl tvp erawtfos kcitsdnim era ereH


---

Original Source: https://www.mindstick.com/forum/156842/how-to-reverse-a-string-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
