Метод StringBuffer append () в Java с примерами

Опубликовано: 13 Февраля, 2022

Pre-requisite: StringBuffer class in Java
The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used:

  1. StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boolean a) is an inbuilt method in Java which is used to append the string representation of the boolean argument to a given sequence.

    Syntax :

    public StringBuffer append(boolean a)

    Parameter : This method accepts a single parameter a of boolean type and refers to the Boolean value to be appended.

    Return Value : The method returns a reference to this object.

    Examples:

    Input: 
    string_buffer = "I love my Country" 
    boolean a = true
    
    Output: I love my Country true
    

    Below program illustrates the java.lang.StringBuffer.append() method:

    // Java praogram to illustrate the
    // StringBuffer append(boolean a)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            StringBuffer sbf1 = new StringBuffer("We are geeks and its really ");
            System.out.println("Input: " + sbf1);
      
            // Appending the boolean value
            sbf1.append(true);
            System.out.println("Output: " + sbf1);
      
            System.out.println();
      
            StringBuffer sbf2 = new StringBuffer("We are lost - ");
            System.out.println("Input: " + sbf2);
      
            // Appending the boolean value
            sbf2.append(false);
            System.out.println("Output: " + sbf2);
        }
    }
    Output:
    Input: We are geeks and its really 
    Output: We are geeks and its really true
    
    Input: We are lost - 
    Output: We are lost - false
    
  2. java.lang.StringBuffer.append(char a) : This is an inbuilt method that appends the string representation of the char argument to the given sequence. The char argument is appended to the contents of this StringBuffer sequence.

    Syntax :

    public StringBuffer append(char a)

    Parameter : The method accepts a single parameter a which is the Char value whose string representation is to be appended.

    Return Value: The method returns a string object after the append operation is performed.
    Examples :

    Input :
    StringBuffer = I love my Country 
    char a = A
    
    Output: I love my Country A

    Below programs illustrate the java.lang.StringBuffer.append(char a) method.

    // Java praogram to illustrate the
    // java.lang.StringBuffer.append(char a)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
            System.out.println("We are geeks and its really ");
            StringBuffer sbf = new StringBuffer("We are geeks and its");
      
            /* Here it appends the char argument as
             string to the string buffer */
            sbf.append("M");
            System.out.println("Result after appending = " + sbf);
      
            System.out.println("We are lost -");
            sbf = new StringBuffer("We are lost -");
      
            /* Here it appends the char argument as
             string to the string buffer */
            sbf.append("&");
            System.out.println("Result after appending = " + sbf);
        }
    }
    Output:



    We are geeks and its really 
    Result after appending = We are geeks and itsM
    We are lost -
    Result after appending = We are lost -&