Reverse string in Java

Following example shows you on how to reverse a String after taking it from command line argument .The program buffers the input String using StringBuffer(String string) method, reverse the buffer and then converts the buffer into a String with the help of toString() method.

public class StringReverse{
   public static void main(String[] args){
      String string="resare";
      String reverse = new StringBuffer(string).
      reverse().toString();
      System.out.println("\nString before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
   }
}
Result:
The above code sample will produce the following result.
String before reverse: resare
String after reverse: eraser


Source code: