Skip to Content

Java StringBuffer append method Example

  1. /*
  2.    Java StringBuffer append method Example
  3.    This example shows how a value can be appended in to StringBuffer object.
  4. */
  5.  
  6. public class JavaStringBufferAppendExample {
  7.  
  8.   public static void main(String[] args) {
  9.    
  10.     /*
  11.     Java StringBuffer class provides following methods to append various
  12.     primitive values and objects to StringBuffer object.
  13.     */
  14.    
  15.     //StringBuffer append(boolean b) method appends boolean to StringBuffer object
  16.     boolean b = true;
  17.     StringBuffer sb1 =  new StringBuffer("BooelanAppended : ");
  18.     sb1.append(b);
  19.     System.out.println(sb1);
  20.    
  21.     //StringBuffer append(char c) method appends character to StringBuffer object
  22.     char c = 'Y';
  23.     StringBuffer sb2 =  new StringBuffer("CharAppended : ");
  24.     sb2.append(c);
  25.     System.out.println(sb2);
  26.    
  27.     /*StringBuffer append(char[] c) method appends character array
  28.     to StringBuffer object*/
  29.     char[] c1 = new char[] {'Y','e','s'};
  30.     StringBuffer sb3 =  new StringBuffer("Character Array Appended : ");
  31.     sb3.append(c1);
  32.     System.out.println(sb3);
  33.  
  34.     //StringBuffer append(double d) method appends double to StringBuffer object
  35.     double d = 1.0;
  36.     StringBuffer sb4 =  new StringBuffer("doubleAppended : ");
  37.     sb4.append(d);
  38.     System.out.println(sb4);
  39.    
  40.     //StringBuffer append(float f) method appends float to StringBuffer object
  41.     float f = 1.0f;
  42.     StringBuffer sb5 =  new StringBuffer("floatAppended : ");
  43.     sb5.append(f);
  44.     System.out.println(sb5);
  45.  
  46.     //StringBuffer append(int i) method appends integer to StringBuffer object
  47.     int i = 1;
  48.     StringBuffer sb6 =  new StringBuffer("integerAppended : ");
  49.     sb6.append(i);
  50.     System.out.println(sb6);
  51.  
  52.     //StringBuffer append(long l) method appends long to StringBuffer object
  53.     long l = 1;
  54.     StringBuffer sb7 =  new StringBuffer("longAppended : ");
  55.     sb7.append(l);
  56.     System.out.println(sb7);
  57.  
  58.     //StringBuffer append(Object o) method appends Object to StringBuffer object
  59.     /* NOTE : Objects are first converted to a String and then it is
  60.     appended to StrinBuffer */
  61.     Object obj = new String("Yes");
  62.     StringBuffer sb8 =  new StringBuffer("ObjectAppended : ");
  63.     sb8.append(obj);
  64.     System.out.println(sb8);
  65.    
  66.     //StringBuffer append(String str) method appends String to StringBuffer object
  67.     String str = new String("Yes");
  68.     StringBuffer sb9 =  new StringBuffer("StringAppended : ");
  69.     sb9.append(str);
  70.     System.out.println(sb9);
  71.    
  72.    
  73.    
  74.   }
  75. }
  76.  
  77. /*
  78. Output Would be
  79.  
  80. BooelanAppended : true
  81. CharAppended : Y
  82. Character Array Appended : Yes
  83. doubleAppended : 1.0
  84. floatAppended : 1.0
  85. integerAppended : 1
  86. longAppended : 1
  87. ObjectAppended : Yes
  88. StringAppended : Yes
  89.  
  90. */