I see a program to reverse a string
public class ReverseName{
public static void main(String[]args)
{
String name=args[0];
String reverse=new StringBuffer(name).reverse().toString();
System.out.println(reverse);
}
}
so what is new StringBuffer(name).reverse().toString(); all about?
Let’s break this down.
First off we create a new StringBuffer (I’d have used StringBuilder as we don’t need thread safety) with the contents of
name. This just allows a more peformant way to append strings but here it’s used for the next part.This calls the reverse method on the StringBuffer which returns a reversed StringBuffer.
Finally this is turned back into a String.