user1218070
user1218070

Reputation:

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

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?

Upvotes: 4

Views: 2522

Answers (5)

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

Actually String is an immutable class, that means once object of String is created its contents cannot be modified. Therefore we use StringBuffer to construct strings. In above example, object of StringBuffer is created with content name, String in name is reversed in same object of StringBuffer(as it is mutable). Again converted to String object and assigned that object to reverse object reference.

Upvotes: 0

Sandeep Manne
Sandeep Manne

Reputation: 6092

You can split that into 3 lines for understanding

StringBuffer reverseBuffer = new StringBuffer(name); // Creating new StringBuffer object
reverseBuffer  = reverseBuffer.reverse(); //Reversing the content using StringBuffer
String reverse = reverseBuffer.toString(); // Converting StringBuffer to String and saving in reverse 

Upvotes: 2

Jim
Jim

Reputation: 22656

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.

Upvotes: 4

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

From the JAVA API

public StringBuffer reverse()

Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed. Let n be the character length of this character sequence (not the length in char values) just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

Note that the reverse operation may result in producing surrogate pairs that were unpaired low-surrogates and high-surrogates before the operation. For example, reversing "\uDC00\uD800" produces "\uD800\uDC00" which is a valid surrogate pair.

Returns: a reference to this object. Since: JDK1.0.2

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html#reverse%28%29

Upvotes: 1

kommradHomer
kommradHomer

Reputation: 4210

just a StringBuffer object reversing a string

You instantiate the StringBuffer object with the "name" String object , then reverse it.

Upvotes: 1

Related Questions