How to Reverse a String in Java Methods, Examples, and Time Complexity Prepared by Rachit Joshi Introduction String reversal is a common Java programming task used in interviews and beginner exercises. Java provides several ways to reverse a string, ranging from loops to built - in methods. Method 1: Using a for Loop String str = "Java"; String rev = ""; for (int i = str.length() - 1; i >= 0; i -- ) { rev += str.charAt(i); } System.out.println(rev); Method 2: Using a while Loop String str = "Java"; String rev = ""; int i = str.length() - 1; while(i>=0){ rev += str.charAt(i); i -- ; } System.out.println(rev); Method 3: Using StringBuilder.reverse() String str="Java"; String rev=new StringBuilder(str).reverse().toString(); System.out.println(rev); This is one of the simplest and most efficient approaches. Method 4: Using StringBuffer.reverse() String str="Java"; String rev=new StringBuffer(str).reverse().toString(); System.out.println(rev); Method 5: Using Recursion static String reverse(String s){ if(s.isEmpty()) return s; return reverse(s.substring(1))+s.charAt(0); } Method 6: Using a Character Array char[] ch=str.toCharArray(); for(int i=ch.length - 1;i>=0;i -- ){ System.out.print(ch[i]); } Method 7: Using a Stack Stack<Character> stack=new Stack<>(); for(char c:str.toCharArray()) stack.push(c); while(!stack.isEmpty()) System.out.print(stack.pop()); Comparison of Methods Method Time Space for Loop O(n) O(n) while Loop O(n) O(n) StringBuilder.reverse() O(n) O(n) StringBuffer.reverse() O(n) O(n) Recursion O(n) O(n) Character Array O(n) O(n) Stack O(n) O(n) Key Points • StringBuilder.reverse() is the easiest and most commonly used approach. • Loops help beginners understand string manipulation. • Recursion demonstrates recursive thinking. • Stack - based reversal follows the LIFO principle. Conclusion Java offers multiple ways to reverse a string, and each method is useful for different learning and programming scenarios. StringBuilder.reverse() is generally the most convenient solution for everyday use.