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.
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 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);
}
Videos
19:58
Frequently Asked Java Program 03: Reverse A String | 3 Ways of ...
01:06
Reverse Strings in JAVA | (simple & easy) - YouTube
01:00
Java Program to Reverse String without using in-built reverse() ...
04:09
Java Program #7 - Reverse a String in Java - YouTube
10:01
String Reverse in Java | Part 1 | Using Loop (Beginner Friendly) ...
04:27
Reverse String in Java 8 Program using For Loop #java #string ...
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
TutorialsPoint
tutorialspoint.com › home › javaexamples › java string reverse example
Java String Reverse Example
September 1, 2008 - The following is an example of reversing a string by using toCharArray() method · import java.io.*; import java.util.*; public class HelloWorld { public static void main(String[] args) { String input = "tutorialspoint"; char[] try1 = ...
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
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
Hackr
hackr.io › home › articles › programming
How to Reverse a String in Java: 9 Ways with Examples [Easy]
January 30, 2025 - In this example we use the StringBuffer class and its built-in reverse() method to quickly return the reverse of a string in Java. This is similar to the StringBuilder approach in method 3 as the StringBuffer is also mutable (can be altered ...
Javatpoint
javatpoint.com › how-to-reverse-string-in-java
How to Reverse String in Java - javatpoint
Java Program to prove that strings are immutable in java · Java program to reverse a given string with preserving the position of spaces
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to reverse a string in java
How to Reverse a String in Java | Interview Kickstart
First, we’ll create a temporary byte array whose length will be equal to the length of the string, and then we’ll store the string in byte form in a reverse manner in that byte array. Now, we again convert it into the string and simply print it. ... Collection class in Java has a built-in reverse() method to reverse the object.
Address 4701 Patrick Henry Dr Bldg 25, 95054, Santa Clara
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 - The following are the nine most convenient ways to reverse a string in Java. Firstly, you must create a new empty string to accommodate the reversed string in this method. Then, you must add each character of the original string in reverse order to the new empty string. ... In the above example, the input is the original string ‘Hello, World’. ‘Reversed’ is the new empty string created to accommodate the reversed string.
Codecademy
codecademy.com › docs › java › stringbuilder › .reverse()
Java | StringBuilder | .reverse() | Codecademy
August 22, 2022 - Returns a modified StringBuilder object with its character sequence rearranged in the opposite order.
Mkyong
mkyong.com › home › java › how to reverse a string in java
How to reverse a string in Java - Mkyong.com
June 4, 2020 - The below code snippet is similar to the StringBuilder(str).reverse() to reverse a string (excepts UTF16 stuff). ... package com.mkyong.crypto.bytes; import java.nio.charset.StandardCharsets; public class ReverseString3 { public static void main(String[] args) { String str = "Hello World"; System.out.println(reverse(str)); } public static String reverse(String input) { if (input == null || input.length() < 0) throw new IllegalArgumentException("Please provide an input!"); byte[] val = input.getBytes(StandardCharsets.UTF_8); int length = val.length - 1; for (int start = (length - 1) >> 1; start >= 0; start--) { int end = length - start; byte temp = val[start]; val[start] = val[end]; val[end] = temp; // debugging //System.out.println(String.format("start=%s, end=%s", start, end)); } return new String(val); } }