Home » Java programming language

Java StringBuffer append(String s) method with Example

Java StringBuffer append(String s) method: Here, we are going to learn about the append(String s) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on June 29, 2019

StringBuffer Class append(String s)

  • This method is available in package java.lang.StringBuffer.append(String s).
  • This method is used to append the specified object with the given object.
  • This method is overridable so it is available in different forms like append(String s) or append(Boolean b) or append(int i) or append(char c) etc.

Syntax:

    StringBuffer append(String s){
    }

Parameter(s):

The return type of this method is StringBuffer that means this method returns a reference to this object.

Return value:

We can pass only one object as a parameter in the method and that object will append in a sequence and object can be of any type.

Java program to demonstrate example of append(String s) method

import java.lang.StringBuffer;

public class Main {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Java");

        // use append(Boolean b) it will append the Boolean 
        // parameter as string to the StringBuffer

        sb.append(true);

        // Display result after appending

        System.out.println("The result will be after appending Boolean object to the StringBuffer is: "+sb);

        sb = new StringBuffer("Version");

        // use append(int i) it will append the Integer parameter 
        // as string to the StringBuffer

        sb.append(8);

        // Display result after appending

        System.out.println("The result will be after appending Integer object to the StringBuffer is: "+sb);
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java StringBufferClass
The result will be after appending Boolean object to the StringBuffer is :Javatrue
The result will be after appending Integer object to the StringBuffer is :Version8


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.