Learn StringBuffer() Class in Java: Lecture 3

This article continues a detailed description of the append() method used in the Java StringBuffer() class.
Before reading further, read the previous parts of the series.
Now let's move forward to the next append() methods to reach our purpose of learning. The next four append() methods are as in the following:
 
  • StringBuffer append(int i) appends the string representation of the int argument to the given sequence. It returns the reference to the given object.
     
  • StringBuffer append(long lng) appends the string representation of the long argument to the given sequence. It also returns the reference to the given object.
     
  • StringBuffer append(float f) appends the string representation of the float argument to the given sequence. It returns the reference to the given object. 
     
  • StringBuffer append(double d) appends the string representation of the double argument to the given sequence. It also returns the reference to the given object.
Now to understand with code examples.

Example
 
The following example contains all four append() methods stated above.
  1. public class AppendIntLongFloatDoubleMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Ben ");  
  6.         System.out.println("Output for append(int i):");  
  7.         System.out.println();  
  8.         str1.append(10);  
  9.         System.out.println(str1); //appends int argument as string to the string buffer  
  10.         str1 = new StringBuffer("Lecture ");  
  11.         str1.append(3);  
  12.         System.out.println(str1); //appends int argument as string to the string buffer  
  13.         System.out.println();  
  14.         System.out.println("Output for append(long lng):");  
  15.         System.out.println();  
  16.         StringBuffer str2 = new StringBuffer("sum = ");  
  17.         str2.append(9632);  
  18.         System.out.println(str2); //appends long argument as string to the string buffer  
  19.         str2 = new StringBuffer("Pin Code: ");  
  20.         str2.append(284003);  
  21.         System.out.println(str2); //appends long argument as string to the string buffer  
  22.         System.out.println();  
  23.         System.out.println("Output for append(float f):");  
  24.         System.out.println();  
  25.         StringBuffer str3 = new StringBuffer("Rate of str3 = ");  
  26.         str3.append(3.147);  
  27.         System.out.println(str3); //appends float argument as string to the string buffer  
  28.         str3 = new StringBuffer("Percentage: ");  
  29.         str3.append(73.143);  
  30.         System.out.println(str3 + "%"); //appends float argument as string to the string buffer  
  31.         System.out.println();  
  32.         System.out.println("Output for append(double d):");  
  33.         System.out.println();  
  34.         StringBuffer str4 = new StringBuffer("Double rate = ");  
  35.         str4.append(60.234134);  
  36.         System.out.println(str4); //appends double argument as string to the string buffer  
  37.         str4 = new StringBuffer("Double Percentage: ");  
  38.         str4.append(5451.0001010303);  
  39.         System.out.println(str4 + "%"); //appends double argument as string to the string buffer  
  40.     }  
  41. }  
Output
 
 
Now the next and the last four append() methods are being discussed in this lecture.
 
 
  • StringBuffer append(Boolean b) appends the string representation of the Boolean argument to the given sequence.
     
  • StringBuffer append(Object obj) appends the string representation of the object argument to the given sequence.

  • StringBuffer append(StringBuffer sb) appends the specified StringBuffer argument to the given sequence. The characters of the StringBuffer arguments are appended in order (thread-safe) to the content of the StringBuffer and increase its (StringBuffer) length by the length of the StringBuffer argument. If the value of sb is “null” then the four characters of null will be appended.
  • StringBuffer appendCodePoint(int codePoint) appends the string representation of the codePoint argument to the given sequence and the argument is appended to the content of the sequence.
Again, we will move to some examples to get a clear picture.
 
Example

Here all four append() methods are illustrated in this example.
  1. public class AppendBooleanStrbuffObjCodePointMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Time = ");  
  6.         System.out.println("Output for append(boolean b):");  
  7.         System.out.println();  
  8.         str1.append(true);  
  9.         System.out.println(str1); //appends the boolean argument as string to the StringBuffer  
  10.         str1 = new StringBuffer("Rest = ");  
  11.         str1.append(false);  
  12.         System.out.println(str1); //appends the boolean argument as string to the StringBuffer  
  13.         System.out.println();  
  14.         System.out.println("Output for append(Object obj):");  
  15.         System.out.println();  
  16.         StringBuffer str2 = new StringBuffer("Lost and ");  
  17.         Object obj1 = "found";  
  18.         str2.append(obj1); //appends object value  
  19.         System.out.println("After appending: " + str2); //print string buffer after appending  
  20.         str2 = new StringBuffer("Gone ");  
  21.         Object obj2 = "forever";  
  22.         str2.append(obj2); //appends object value  
  23.         System.out.println("After appending: " + str2); //print string buffer after appending  
  24.         System.out.println();  
  25.         System.out.println("Output for append(StringBuffer sb):");  
  26.         System.out.println();  
  27.         StringBuffer str3 = new StringBuffer("Oh my ");  
  28.         StringBuffer str4 = new StringBuffer("God");  
  29.         System.out.println("Buffer 1st is: " + str3);  
  30.         System.out.println("Buffer 2nd is: " + str4);  
  31.         str3.append(str4); //appends strBuff4 to strBuff3  
  32.         System.out.println("After appending: " + str3); //prints StringBuffer after appending  
  33.         str3 = new StringBuffer("143");  
  34.         str4 = new StringBuffer("<->1432");  
  35.         StringBuffer str5 = new StringBuffer("*01010");  
  36.         System.out.println("Buffer 1st is: " + str3);  
  37.         System.out.println("Buffer 2nd is: " + str4);  
  38.         System.out.println("Buffer 3rd is: " + str5);  
  39.         str3.append(str4).append(str5); //appends strBuff5 & strBuff4 to strBuff3  
  40.         System.out.println("After appending: " + str3); //prints StringBuffer after appending  
  41.         System.out.println();  
  42.         System.out.println("Output for appendCodePoint(int codePoint):");  
  43.         System.out.println();  
  44.         StringBuffer str6 = new StringBuffer("Pin");  
  45.         System.out.println("Buffer is: " + str6);  
  46.         str6.appendCodePoint(103); //appends codePoint as string to the StringBuffer  
  47.         System.out.println("After appending: " + str6);  
  48.         /*prints the string buffer after appending 103 
  49. as codePoint*/  
  50.         str6.appendCodePoint(115);  
  51.         System.out.println("After appending: " + str6);  
  52.         /*prints the string buffer after appending 103 & 115 
  53. as codePoints*/  
  54.     }  
  55. }  
Output:
 
 
The next lecture will be about other methods of the StringBuffer() class and it will be here as soon as possible, until then, thank you, keep learning and sharing.


Similar Articles