reverse string algorithm java code example

Example 1: reverse string java

// Use StringBuilder for non-thread environment, it is faster
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);

Example 2: Reverse a string in java

// reverse a string using character array
class ReverseUsingCharacterArray
{
   public static void main(String[] args)
   {
      String str = "HelloWorldJava";
      char[] ch = str.toCharArray();
      for(int a = ch.length - 1; a >= 0; a--)
         System.out.print(ch[a]);
   }
}

Tags:

Java Example