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.
- public class AppendIntLongFloatDoubleMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Ben ");
- System.out.println("Output for append(int i):");
- System.out.println();
- str1.append(10);
- System.out.println(str1);
- str1 = new StringBuffer("Lecture ");
- str1.append(3);
- System.out.println(str1);
- System.out.println();
- System.out.println("Output for append(long lng):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("sum = ");
- str2.append(9632);
- System.out.println(str2);
- str2 = new StringBuffer("Pin Code: ");
- str2.append(284003);
- System.out.println(str2);
- System.out.println();
- System.out.println("Output for append(float f):");
- System.out.println();
- StringBuffer str3 = new StringBuffer("Rate of str3 = ");
- str3.append(3.147);
- System.out.println(str3);
- str3 = new StringBuffer("Percentage: ");
- str3.append(73.143);
- System.out.println(str3 + "%");
- System.out.println();
- System.out.println("Output for append(double d):");
- System.out.println();
- StringBuffer str4 = new StringBuffer("Double rate = ");
- str4.append(60.234134);
- System.out.println(str4);
- str4 = new StringBuffer("Double Percentage: ");
- str4.append(5451.0001010303);
- System.out.println(str4 + "%");
- }
- }
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.
ExampleHere all four append() methods are illustrated in this example.
- public class AppendBooleanStrbuffObjCodePointMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Time = ");
- System.out.println("Output for append(boolean b):");
- System.out.println();
- str1.append(true);
- System.out.println(str1);
- str1 = new StringBuffer("Rest = ");
- str1.append(false);
- System.out.println(str1);
- System.out.println();
- System.out.println("Output for append(Object obj):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Lost and ");
- Object obj1 = "found";
- str2.append(obj1);
- System.out.println("After appending: " + str2);
- str2 = new StringBuffer("Gone ");
- Object obj2 = "forever";
- str2.append(obj2);
- System.out.println("After appending: " + str2);
- System.out.println();
- System.out.println("Output for append(StringBuffer sb):");
- System.out.println();
- StringBuffer str3 = new StringBuffer("Oh my ");
- StringBuffer str4 = new StringBuffer("God");
- System.out.println("Buffer 1st is: " + str3);
- System.out.println("Buffer 2nd is: " + str4);
- str3.append(str4);
- System.out.println("After appending: " + str3);
- str3 = new StringBuffer("143");
- str4 = new StringBuffer("<->1432");
- StringBuffer str5 = new StringBuffer("*01010");
- System.out.println("Buffer 1st is: " + str3);
- System.out.println("Buffer 2nd is: " + str4);
- System.out.println("Buffer 3rd is: " + str5);
- str3.append(str4).append(str5);
- System.out.println("After appending: " + str3);
- System.out.println();
- System.out.println("Output for appendCodePoint(int codePoint):");
- System.out.println();
- StringBuffer str6 = new StringBuffer("Pin");
- System.out.println("Buffer is: " + str6);
- str6.appendCodePoint(103);
- System.out.println("After appending: " + str6);
-
-
- str6.appendCodePoint(115);
- System.out.println("After appending: " + str6);
-
-
- }
- }
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.