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.
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
W3Schools
w3schools.com › Java › java_howto_reverse_string.asp
Java How To Reverse a String - W3Schools
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
Videos
19:58
Frequently Asked Java Program 03: Reverse A String | 3 Ways of ...
11:53
Reverse String in Java Practice Program - YouTube
10:26
How to Reverse a String in Java - YouTube
01:00
Java Program to Reverse String without using in-built reverse() ...
Reverse Strings in JAVA | (simple & easy)
GeeksforGeeks
geeksforgeeks.org › java › reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
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().
Published October 14, 2025
Top answer 1 of 16
1224
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.
2 of 16
128
For Online Judges problems that does not allow StringBuilder or StringBuffer, you can do it in place using char[] as following:
public static String reverse(String input){
char[] in = input.toCharArray();
int begin=0;
int end=in.length-1;
char temp;
while(end>begin){
temp = in[begin];
in[begin]=in[end];
in[end] = temp;
end--;
begin++;
}
return new String(in);
}
TutorialsPoint
tutorialspoint.com › home › javaexamples › java string reverse example
How to Reverse a String in Java
September 1, 2008 - Reversing a string is a common operation that can be done in multiple ways. Java provides both built-in methods and manual approaches to reverse a string. We can reverse a string using StringBuffer, StringBuilder, iteration, etc.
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
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
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
W3Schools
w3schools.com › java › java_ref_string.asp
Java String Reference
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
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.
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
Codecademy
codecademy.com › docs › java › stringbuilder › .reverse()
Java | StringBuilder | .reverse() | Codecademy
August 22, 2022 - The .reverse() method returns a modified StringBuilder object with its character sequence rearranged in the opposite order. This is the most straightforward way to reverse a string in Java.
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.
Top answer 1 of 3
2
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"
2 of 3
1
Recursive methods call themselves repeatedly till some conditions are met when they start exiting. You might want to dry run the code or just put a debug at start of this method to print the string value and it would be fairly easy to see how the called value of string changes with each call also the point where the termination happen.