what is 'new StringBuffer(aString)' in java?

java

Solution

String reverse=new StringBuffer(name).reverse().toString();

Let's break this down.

new StringBuffer(name)

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.

.reverse()

This calls the reverse method on the StringBuffer which returns a reversed StringBuffer.

.toString();

Finally this is turned back into a String.

Problem

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?

Original source

Related problems