Java.lang.StringBuffer.append() Method

Advertisements


Description

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

Declaration

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

public StringBuffer append(StringBuffer sb)

Parameters

  • sb -- This is the StringBuffer to append.

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 buff1 = new StringBuffer("tutorials ");
    System.out.println("buffer1 = " + buff1);
    StringBuffer buff2 = new StringBuffer("point ");
    System.out.println("buffer2 = " + buff2);
     
    // appends stringbuffer2 to stringbuffer1
    buff1.append(buff2);
    // print the string buffer after appending
    System.out.println("After append = " + buff1);
  }
}

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

buffer1 = tutorials
buffer2 = point
After append = tutorials point 



Advertisements
Advertisements