Add value to StringBuffer with append method - Java Language Basics

Java examples for Language Basics:StringBuffer

Description

Add value to StringBuffer with append method

Demo Code


public class Main {
 
  public static void main(String[] args) {
    //StringBuffer append(boolean b) method
    boolean b = true;
    StringBuffer sb1 =  new StringBuffer("BooelanAppended : ");
    sb1.append(b);//w  ww . j a v  a  2 s .c  o m
    System.out.println(sb1);
   
    //StringBuffer append(char c) method
    char c = 'Y';
    StringBuffer sb2 =  new StringBuffer("CharAppended : ");
    sb2.append(c);
    System.out.println(sb2);
   
    /*StringBuffer append(char[] c) method
    to StringBuffer object*/
    char[] c1 = new char[] {'Y','e','s'};
    StringBuffer sb3 =  new StringBuffer("Character Array Appended : ");
    sb3.append(c1);
    System.out.println(sb3);
 
    //StringBuffer append(double d) method
    double d = 1.0;
    StringBuffer sb4 =  new StringBuffer("doubleAppended : ");
    sb4.append(d);
    System.out.println(sb4);
   
    //StringBuffer append(float f) method
    float f = 1.0f;
    StringBuffer sb5 =  new StringBuffer("floatAppended : ");
    sb5.append(f);
    System.out.println(sb5);
 
    //StringBuffer append(int i) method
    int i = 1;
    StringBuffer sb6 =  new StringBuffer("integerAppended : ");
    sb6.append(i);
    System.out.println(sb6);
 
    //StringBuffer append(long l) method
    long l = 1;
    StringBuffer sb7 =  new StringBuffer("longAppended : ");
    sb7.append(l);
    System.out.println(sb7);
 
    //StringBuffer append(Object o) method
    /* NOTE : Objects are first converted to a String and then it is
    appended to StrinBuffer */
    Object obj = new String("Yes");
    StringBuffer sb8 =  new StringBuffer("ObjectAppended : ");
    sb8.append(obj);
    System.out.println(sb8);
   
    //StringBuffer append(String str) method
    String str = new String("Yes");
    StringBuffer sb9 =  new StringBuffer("StringAppended : ");
    sb9.append(str);
    System.out.println(sb9);
  }
}

Result


Related Tutorials