Java.lang.StringBuffer.toString() Method

Advertisements


Description

The java.lang.StringBuffer.toString() method returns a string representing the data in this sequence.
A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned.

Declaration

Following is the declaration for java.lang.StringBuffer.toString() method

public String toString()

Parameters

  • NA

Return Value

This method returns a string representation of this sequence of characters.

Exception

  • NA

Example

The following example shows the usage of java.lang.StringBuffer.toString() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {
 
  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("India ");
    System.out.println("buffer = " + buff);

    // append character to the stringbuffer
    buff.append('!');
    // convert to string object and print it
    System.out.println("After append = " + buff.toString());
      
    buff = new StringBuffer("Hi "); 
    System.out.println("buffer = " + buff);
    // append integer to the stringbuffer
    buff.append(123);
    // convert to string object and print it
    System.out.println("After append = " + buff.toString());
  }
}

Let us compile and run the above program, this will produce the following result:

buffer = India
After append = India !
buffer = Hi
After append = Hi 123



Advertisements
Advertisements