java.lang.StringBuffer append(CharSequence s)

Description :

This java tutorial shows how to use the append(CharSequence s) method of StringBuffer class under java.lang package. The append method of StringBuffer class is overloaded which takes different data type as input however on this case the input is CharSequence data type thus a character sequence is appended on the base string.

Method Syntax :

public StringBuffer append(CharSequence s)

Parameter Input :

DataType Parameter Description
CharSequence s This will be appended on the base string.

Method Returns :

The append(CharSequence s) method simply returns the reference object which is the combined String representation of the base string and the CharSequence method input. Make a note that if the method input is null, the string “null” is appended on the base string.

Compatibility Version :

Requires Java 1.5 and up

Java Code Example :

This java example source code demonstrates the use of append(CharSequence s) method of StringBuffer class. Basically this code just takes a user input and then the program will translate it into boolean value and eventually append the translated user input to the original String on the buffer. This source code is a good example in learning on how to initialize StringBuffer object, modify the contents of the StringBuffer object and printing of the current String on the buffer.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

import java.io.IOException;
import java.util.Scanner;

/*
 * This example source code demonstrates the use of append(CharSequence s)
 * of StringBuffer class
 */

public class StringBufferAppendCharSequence {

	public static void main(String[] args) throws IOException {

		String name = "Andrew";
		CharSequence lastName = "Carnegie"; 

		// initialize the StringBuffer object
		StringBuffer sb = new StringBuffer(name + "|");
		sb.append(lastName);
		System.out.println("Contents of Buffer:" + sb.toString());

	}

}

Sample Output :

Running the append(CharSequence s) method example source code of StringBuffer class will give you the following output

Contents of Buffer:Andrew|Carnegie

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :