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.
Software Testing Help
softwaretestinghelp.com › home › java › java reverse string: tutorial with programming examples
Java Reverse String: Tutorial With Programming Examples
April 1, 2025 - In this scenario, we will use the Split() method to split each character of a String and using for loop, we will print each character in the reverse order of occurrence. Here, we have taken the input through the console using the Scanner Class. import java.util.Scanner; public class Reverse { public static void main(String[] args) { String str; // Taking input through the console using Scanner Class Scanner in = new Scanner(System.in); System.out.println("Enter your String"); str = in.nextLine(); /* * Splitted each character of the String and then * printed the same in the reverse order using * for loop */ String[] split = str.split(""); for(int i=split.length-1; i>=0; i--) { System.out.print(split[i] + ""); } } }
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
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
[JAVA] Is there a way to make this reverse method more efficient? Possibly requiring only one loop?
Why are you iterating from 0 to size and then have your calculation? You could directly iterate in the opposite direction Since you have index-based access to the list elements, you should be able to do an in-place swap via a third, temp element (single element, not a list). In this case, you only need a single loop and only need to iterate over half the list. More on reddit.com
The use of Java's Reverse() method
Easiest way I can currently think of would be to just do (n < 0 ? -1 : 1) * Integer.parseInt(String.valueOf(Math.abs(n)).reverse()) More on reddit.com
Videos
13:03
Reverse a String in Java - 4 Best Ways | Java Interview Question ...
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) ...
Reverse Strings in JAVA | (simple & easy)
04:38
Java 8 Stream Program to Reverse Each Word of String | Frequently ...
GeeksforGeeks
geeksforgeeks.org › java › reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: StringBuilder objects are mutable, and their reverse() method reverses the content in-place, which is faster than manual looping. 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 May 12, 2026
GeeksforGeeks
geeksforgeeks.org › java › collections-reverse-method-in-java-with-examples
Collections.reverse() Method in Java with Examples - GeeksforGeeks
Reverses the order of elements in a given List. Works in-place, meaning it updates the original list instead of creating a new one. Commonly used for changing display or processing order of data.
Published April 14, 2026
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-reverse-method-in-java
StringBuffer reverse() Method in Java with Examples - GeeksforGeeks
August 4, 2022 - The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence.
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_reverse.htm
Java StringBuilder reverse() Method
The Java StringBuilder reverse() method is used to reverse the characters of a StringBuilder object. It replaces the sequence of characters in reverse order. The method is a static method with predefined logic for reversing a string in Java.
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:
CodeGym
codegym.cc › java blog › random › different ways to reverse a string in java
Different Ways to Reverse a String in Java
February 1, 2023 - We can also reverse the string in Java by using the StringBuilder class. The StringBuilder class provides a reverse() method that can be used to reverse a string. The reverse() method of the Java StringBuilder class is used to replace this character sequence with the reverse of the sequence.
Tutorialspoint
tutorialspoint.com › java › stringbuffer_reverse.htm
Java - String Buffer reverse() Method
This method reverses the value of the StringBuffer object that invoked the method. Let n be the length of the old character sequence, the one contained in the string buffer just prior to the execution of the reverse method.
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › reverse string in java
Reverse String in Java | Using Various Methods with Examples
January 8, 2024 - It reverses the order of the characters using the reverse() method from the Collections class. Following the reversal, an iterator is used to iterate over the list and print the reversed string.
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to reverse a string in java
How to Reverse a String in Java | Interview Kickstart
September 25, 2024 - getBytes() method of String is used to convert the string into bytes, and we will use this method to reverse the string. 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.
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
TutorialsPoint
tutorialspoint.com › java › util › collections_reverse.htm
Java Collections reverse() Method
The Java Collections reverse(List<?>) method is used to reverse the order of the elements in the specified list.
GeeksforGeeks
geeksforgeeks.org › java › integer-reverse-method-in-java
Integer reverse() Method In Java - GeeksforGeeks
July 7, 2018 - The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value.
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-reverse-in-java-with-examples
StringBuilder reverse() in Java with Examples - GeeksforGeeks
January 22, 2026 - public class GFG{ public static ... object so it can be modified. The reverse() method reverses all characters in the sentence, and the result is printed to the console....
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.