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
🌐
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
Discussions

how to reverse a string in java
Write down a short string on a piece of paper and reverse the string by hand. How do you do that? More on reddit.com
🌐 r/learnprogramming
11
1
April 4, 2021
How to reverse a string in Java? - TestMu AI Community
How to reverse a string in Java? I have a string Hello World" stored in a variable and need to print it in reverse. I heard that Java provides built-in methods for this. What is the best way to java reverse string efficiently? Additionally, how can I reverse each individual word in “Hello ... More on community.testmu.ai
🌐 community.testmu.ai
0
February 6, 2025
Explanation for how this recursive method to reverse a string works (JAVA)
Try walking through it on paper. Here's an algorithm to follow: Reverse: input is "str". If str is blank, return blank. separate str into the first character, car, and the remaining characters, cdr. Invoke this algorithm with an input of cdr, label result "reversedCdr". Return reversedCdr + car. If you do this by hand, you'll see what's happening. Make sure to carefully track the stack. Here's an example. Reverse(str is "nab") str isn't blank, keep going car is "n", cdr is "ab". Call reverse(str is "ab") str isn't blank, keep going car is "a", cdr is "b" Call reverse(str is "a") str isn't blank, keep going car is "b", cdr is "" Call reverse(str is "") str is blank, return blank. Return "" + "b", "b" Return "b" + "a", "ba" Return "ba" + "n", "ban" More on reddit.com
🌐 r/learnprogramming
3
5
February 21, 2021
Challenge: Reverse a string in place
I think the trick to this is using XOR to switch characters in the string without having to use temporary variables to store them. That is, one can switch two variables x and y by using: x = x^y; y = x^y; x = x^y; C++: #include int main(){ char string[]="String to reverse"; std::cout << string << std::endl; int i; int stringsize= sizeof(string) - 1; for(i=0;i More on reddit.com
🌐 r/programmingchallenges
65
22
January 21, 2011
🌐
W3Schools
w3schools.com › java › java_howto_reverse_string.asp
Java How To Reverse a String
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Reddit
reddit.com › r/learnprogramming › how to reverse a string in java
r/learnprogramming on Reddit: how to reverse a string in java
April 4, 2021 -

hey guys i need your help in java so i have a very beginner problem and i'll probably be laughed at for asking this but how do you reverse a string in java

so far the online called I've been given to work with is:

import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
char[] arr = text.toCharArray();

//your code goes here

}
}

i don't know how to go about it, all i have to do is reverse the string. uhg it was so easy in python with just the (::-) is that how its written idk it's been some time python.

anyways i would appreciate any help and advice on learning java effectively i don't know much yet, i tried going on leetcode but daaaaamn it was crazy hard, i didn't manage to do any problem. thank you

Top answer
1 of 2
1
The previous solution given will work! I'm going to suggest some modifications. Here's the original:public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.substring(i, i + 1); } return reversed;}That call to str.substring(i, i + 1) is essentially a way of picking out a single character at index i, but there's actually a built in String method for that: charAt. And I think things read a bit more easily using it. Using it is also a bit semantically clearer in my opinion (others can disagree!):public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } return reversed;}It's generally better to use a StringBuilder, though, since that keeps us from generating a lot of Strings along the way (what if our input String was a very long String)?public static String reverse(String str) { StringBuilder reversed = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { reversed.insert(0, str.charAt(i)); } return reversed.toString();}Note that here, I actually iterated forward through the String and prepended (using insert), but you could iterate backwards and use the append method, too. But the main point is that using a StringBuilder keeps us from having to generate new Strings for each character in the input.I hope that helps!
2 of 2
0
We can't use a pre-defined function in java that can instantaneously reverse a string. Therefore, we must come up with a way to do so. One way we can do this is by creating a new string adding individual characters to it by traversing through the original string in reverse order. This will result in the final string being the reverse of the original string, and this will be our solution to the problem. An implementation of this solution looks like:public class MyClass {public static void main(String args[]) {String before="hello";String after=reverse(before);System.out.println(after);}public static String reverse(String str){String reversed="";for(int i=str.length()-1; i>=0; i--){reversed+=str.substring(i,i+1);}return reversed;}}
🌐
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
🌐
Medium
medium.com › womenintechnology › 3-ways-to-reversing-string-in-java-b40a23bbcc6c
3 ways to Reversing String In JAVA | by Daily Debug | Women in Technology | Medium
August 10, 2023 - String reveresed = str.chars().mapToObj( c -> (char)c). reduce("",(sa,c)->c+sa,(s1,s2)->s2+s1);System.out.println("Reversed String :"+reveresed); It’s also common in interviews that they ask to reverse a sentence without disturbing its sequence.
Find elsewhere
🌐
Quora
quora.com › In-Java-8-how-can-we-reverse-a-single-string-using-lambdas-and-streams
In Java 8, how can we reverse a single string using lambdas and streams? - Quora
Answer (1 of 5): I don’t know why you need a Lambda here. You can just do - [code]String abc = "abc"; System.out.println(new StringBuilder(abc).reverse().toString()); [/code]But if you do want to use Stream and Lambda you can do something like - [code]String abc = "abc"; String reverse = Arrays...
🌐
GeeksforGeeks
geeksforgeeks.org › java › reverse-words-given-string-java
Reverse words in a given String in Java - GeeksforGeeks
January 22, 2026 - Input: "Welcome to geeksforgeeks" ... between words and at the start or end of the string · Step 1: Split the string into words using a regex pattern for whitespace....
🌐
Emeritus
emeritus.org › home › blog › information technology › 9 most convenient ways to reverse a string in java
9 Most Convenient Ways to Reverse a String in Java
September 24, 2024 - In the above example, a for-each iterative loop converts the input string into a list of characters. Next, the ‘Collections.reverse()’ method reverses the character list. Lastly, the reversed character list converts back to a string using a StringBuilder class and another iterative for-each loop. ALSO READ: Here Are 5 Important Reasons to Learn Java Programming in 2024
🌐
Coderanch
coderanch.com › t › 545713 › java › string-reverse
string reverse O(N) or O(N/2) (Java in General forum at Coderanch)
July 17, 2011 - I think we cant reduce the string reverse to O(N/2)? Please correct me if i am wrong. Thanks Chandrashekar ... Note that there is no O(N/2) - it is linear complexity, and asymptotically the same as O(N). In this particular case, performance is probably improved much more by using a StringBuilder instead of string concatenation. ... Hi, In java String is stored as a char array; in java size of the array is predetermined and stored in head so getting the size of the array or string will be O(1).
🌐
JanBask Training
janbasktraining.com › community › java › reverse-a-string-in-java
Reverse a string in Java | JanBask Training Community
September 14, 2025 - The easiest and most efficient approach is to use StringBuilder’s built-in .reverse() method. String text = "Hello Java"; StringBuilder sb = new StringBuilder(text); String reversed = sb.reverse().toString(); System.out.println(reversed); ...
🌐
Medium
medium.com › @AlexanderObregon › javas-stringbuilder-reverse-method-explained-b2b701ee2029
Java’s StringBuilder.reverse() Method Explained | Medium
October 3, 2024 - In Java, working with strings efficiently is an important skill for any developer. One method that comes in handy when manipulating strings is reverse(), part of the StringBuilder class in the java.lang package. This method allows you to reverse the order of characters in a string, a useful operation for various tasks such as checking palindromes or reversing data streams.
🌐
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 - Java also offers some mechanisms like StringBuilder and StringBuffer that create a mutable sequence of characters. These objects have a reverse() method that helps us achieve the desired result. Here, we need to create a StringBuilder from the String input and then call the reverse() method:
🌐
Hero Vired
herovired.com › learning-hub › blogs › reverse-a-string-in-java
How to Reverse a String in Java Using for Loop | Hero Vired
March 19, 2024 - When you reverse a string in Java, it will change the order of a given string. The change will ensure that the last character of the Java string becomes the first one. Additionally, a Java program to reverse a string also enables you to check the Palindrome of the given string.
🌐
TestMu AI Community
community.testmu.ai › ask a question › testautomation
How to reverse a string in Java? - TestMu AI Community
February 6, 2025 - How to reverse a string in Java? I have a string Hello World" stored in a variable and need to print it in reverse. I heard that Java provides built-in methods for this. What is the best way to java reverse string effici…
🌐
FavTutor
favtutor.com › blogs › reverse-string-java
Reverse a String in Java (with Examples)
September 28, 2024 - In this method, we will convert the original string into a character array using the function toCharArray(). Later, we will keep two-pointer low and high where low belong to starting index and high belongs to the ending index. By using the XOR (^) operator we will swap the first and last character, the second and second last character, and so on and print the reverse string by iterating through array characters as shown below ... import java.util.*; public class ReverseStringByFavTutor { public static void main(String[] args) { String stringExample = "FavTutor"; System.out.println("Original st
🌐
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
🌐
Reddit
reddit.com › r/learnprogramming › explanation for how this recursive method to reverse a string works (java)
r/learnprogramming on Reddit: Explanation for how this recursive method to reverse a string works (JAVA)
February 21, 2021 -
public String reverse(String str) {
    if(str.length()==0){
        return str;
    }
    else{
        return reverse(str.substring(1))+str.charAt(0);
    }
}

I was wondering how recursive methods work. What causes this method continue to plug itself back into the function? When does str.length==0 and why does it end up returning a reversed string rather than a blank in my main method.