Java.lang.StringBuffer.append() Method

Advertisements


Description

The java.lang.StringBuffer.append(String str) method appends the specified string to this character sequence.The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.

Declaration

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

public StringBuffer append(String str)

Parameters

  • str -- This is the value of a string.

Return Value

This method returns a reference to this object.

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("tutorials ");
    System.out.println("buffer = " + buff);
    
    // appends the string argument to the string buffer
    buff.append("point");
    // print the string buffer after appending
    System.out.println("After append = " + buff);
    
    buff = new StringBuffer("1234 ");
    System.out.println("buffer = " + buff);
    // appends the string argument to the string buffer 
    buff.append("!#$%");
    // print the string buffer after appending
    System.out.println("After append = " + buff);
  }
}

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

buffer = tutorials
After append = tutorials point
buffer = 1234
After append = 1234 !#$%



Advertisements
Advertisements