You can use this:

new StringBuilder(hi).reverse().toString()

StringBuilder was added in Java 5. For versions prior to Java 5, the StringBuffer class can be used instead — it has the same API.

Answer from Daniel Brockman on Stack Overflow
🌐
YouTube
youtube.com › nelson darwin pak tech
how to reverse a string in java eclipse | how to invert a string in java in eclipse - YouTube
In this tutorial, you will learn1. how to inverse a string in java eclipse.2. how to revert a string in java eclipse.3. simplest code to inverse a string a s...
Published   October 13, 2022
Views   509
🌐
GeeksforGeeks
geeksforgeeks.org › java › reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: Characters are stored in a list and reversed using Collections.reverse(). This approach is helpful when you’re already working with Java collections. StringBuffer is similar to StringBuilder but thread-safe.
Published   October 14, 2025
Top answer
1 of 4
2

Problem in Q=Q-- and ; symbol after for cylce. Try this:

class Play{
    void REVERSE (){
        String [] INPUT_WORD = {"T","R","A","I","N"};
        int Q;
        for(Q=INPUT_WORD.length-1; Q>=0; Q--) {
            System.out.print(INPUT_WORD[Q]);
        }
    }
    public static void main(String[]args){
        Play PL = new Play();
        PL.REVERSE();
    }
}
2 of 4
1

I'd like to offer a few suggestions.

  1. Indent your code. It not only makes it easier for you to follow, but makes it easier for others to read your code.

  2. Naming conventions. Use Title case for classes, camelCase for both variables and methods, and UPPER_CASE for constants.

  3. Strings and characters. A String can be decomposed into an array of characters with the built-in method, String.toCharArray(). A character array is mutable, so is often used as an intermediate structure when converting a String from one state to another for tasks like ciphers or interview problems.

  4. Encapsulation. If you can make your methods use only what is submitted to them through their method signature, and only output their return value, it's usually best. Prefer passing values over referencing constants in your utility methods to make them easier to follow.

    package abnpackage;
    class Play {
      private static final String INPUT_WORD = "TRAIN";
    
      private String reverse(String word) {
        char[] letters=word.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (int q=letters.length-1; q>=0; q--) {
          sb.append(letters[q]);
        }
        return sb.toString();
      }
    
      public static void main(String[]args) {
        Play play = new Play();
        System.out.println("REVERSE VALUE: " + play.reverse(INPUT_WORD));
      }
    }
    
🌐
Javatpoint
javatpoint.com › how-to-reverse-string-in-java
How to Reverse String in Java - javatpoint
There are many ways to reverse String in Java. We can reverse String using StringBuffer, StringBuilder, iteration etc. Let's see the ways to reverse String in Java.
🌐
FavTutor
favtutor.com › blogs › reverse-string-java
Reverse a String in Java (with Examples)
September 28, 2024 - Using this method, we will instantiate a Stack object of characters and push all the characters of the original string into the stack using the stack’s inbuilt function push(). Since stack follows the principle of “First In Last Out”, characters will be popped out the characters in reversed order. Hence, we will create a new string and pop all characters from the stack, and concatenate them in the new string as shown in the below example ... import java.util.Stack; public class ReverseStringByFavTutor { public static void main(String[] args) { String stringExample = "FavTutor"; System.ou
🌐
W3Schools
w3schools.com › java › java_howto_reverse_string.asp
Java How To Reverse a String
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Sololearn
sololearn.com › en › Discuss › 2805778 › how-to-reverse-a-string-in-java
How to reverse a string in java | Sololearn: Learn to code for FREE!
If you prefer not to use an array and keep it simple with just Strings, then: String myWord = "abc"; String reversed = ""; for (int i = myWord.length() - 1; i >= 0; i--) reversed += myWord.charAt(i); System.out.println(reversed);
Find elsewhere
🌐
Simplilearn
simplilearn.com › home › resources › software development › how to reverse a string in java: 12 best methods
How to Reverse a String in Java: 12 Best Methods
May 5, 2025 - How to Reverse a String in Java? 1. Using toCharArray() 2. Using StringBuilder 3. Using While Loop/For Loop 4. Converting a String to Bytes 5. Using ArrayList
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Programming Simplified
programmingsimplified.com › java › source-code › java-program-reverse-string
Reverse a string in Java | Programming Simplified
Download Reverse string program class file. ... class InvertString { public static void main(String args[]) { StringBuffer a = new StringBuffer("Java programming is fun"); System.out.println(a.reverse()); } }
🌐
coderolls
coderolls.com › reverse-a-string-in-java
How To Reverse A String In Java (5 ways) - Coderolls
December 2, 2019 - In the for loop, append the reversedString with the ith element of the stringCharArray. Print the reversedString. I have given the program as per the above logic. /** * A Java program to reverse a string. * * We are using 'toCharArray' method of the String class to get char array of the specified string * and append it with the temp string in reverse order.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › reverse-a-string-in-java
Reverse a String in Java? - A Complete Guide
In the above example, a new string called 'reversed' is being created, and then a loop is used to repeat over the characters of the original string 'str' from the end to the beginning. Each character must be added to the 'reversed' string in reverse order using the '+=' operator. Finally, return the 'reversed' string. In the main Java Method, call the 'reverse' method with an example string and print out the original and reversed string
🌐
YouTube
youtube.com › mbsbaquagamer
Eclipse java Application Reverse a String - YouTube
A Simple Java Application in eclipse to reverse a string
Published   March 18, 2013
Views   12K
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › reverse a string in java
Reverse a String in Java | 10 Best Methods with Code
October 24, 2025 - By pushing each character of the string onto a stack and then popping it off, you can reverse the string. This method illustrates the stack data structure but is less efficient due to extra space used by the stack.
🌐
Edureka
edureka.co › blog › reverse-a-string-in-java
How to Reverse A String In Java | Java Reverse Programs | Edureka
March 14, 2023 - In the below given Java program will help you to understand how to reverse a String entered by the user. Here, I have used the CharAt() method in order to extract the characters from the input String. The main task of the CharAt() method is to return the character at the specified index in the given String.
🌐
Scaler
scaler.com › home › topics › reverse a string in java
Reverse a String in Java - Scaler Topics
March 14, 2024 - Reversing a string in Java by extracting each character and adding it in front of the existing string is a straightforward approach. This method iterates through the input string, takes each character, and places it at the beginning of a new string.
🌐
Baeldung
baeldung.com › home › java › java string › how to reverse a string in java
How to Reverse a String in Java | Baeldung
January 8, 2024 - Typically, we can use the IntStream.range() method to reverse a particular string in a more functional style.
🌐
Learn Java
javatutoring.com › reverse-a-string-in-java
Reverse A String In Java – 4 Ways | Programs
January 12, 2026 - If block adds the characters to the string word until we will get space i.e. word contains the 1st word. b) If we get space then the else block to reverse the word which is read using else block. 3) 2nd for loop reverse the last word which is read by else block. ... © 2026. Copyrighted Protected. Duplication or Copying Our Site Content Is Strictly Prohibited. However, Reference Links Are Allowed To Our Original Articles - JT. ... Java program to print X star pattern program – We have written the below print/draw ...
🌐
Crunchify
crunchify.com › interview questions answers › how to reverse a string in java? total 7 different ways…
How to Reverse a String in Java? Total 7 different ways... • Crunchify
February 1, 2021 - Good example of reverse in Java – just thought to share that there could be a flavour where you need to reverese the sentence and not the words of the sentence. Recursion would be a great solution though there are other solution as well · private static String reverseMe(String s) { System.out.println("logger ::" + s); int i = s.indexOf(" ");// getting the index of space which is delimeter in case of sentence return i==-1 ?