This article helps you to learn some important methods used in the Java StringBuffer() class with a variety of examples.
Before reading further, read the previous part of this series.
Now in this lecture, we will learn about an important Java StringBuffer() class method in detail, StringBuffer append().
Method: Java StringBuffer append()
Basically this method appends the specified string with the given string. It creates a new StringBuffer initialized to the contents of the specified string.
It is also overloaded by the following parameters:
- StringBuffer append(String str)
- StringBuffer append(char c)
- StringBuffer append(char[] str)
- StringBuffer append(char[] str, int offset, int len)
- StringBuffer append(CharSequence s)
- StringBuffer append(CharSequence s, int start, int end)
- StringBuffer append(double d)
- StringBuffer append(float f)
- StringBuffer append(int i)
- StringBuffer append(long lng)
- StringBuffer append(Object obj)
- StringBuffer append(StringBuffer sb)
- StringBuffer appendCodePoint(int codePoint)
- ·StringBuffer append(boolean b)
We will explain all the overloaded parameters by this method, taking them into various groups with examples.
First, we will explain the top four from the preceding list, because it will be very effective in understanding and remembering the things if we move down slowly instead of moving quickly and it becomes hotchpotch.

StringBuffer append(String str) appends the specified string to the given character sequence. The characters of the string arguments are appended in order (thread-safe) increasing the length of the given sequence by the length of the argument. If the string str is null then only the four characters “null” will be appended.
StringBuffer append(char c) appends the string representation of the char (character) argument to the given sequence. The argument is appended to the contents of the given sequence by increasing its (sequence) length by 1.
StringBuffer append(char[] str) appends the string representation of the char array, in other words, string argument to the given sequence. The char array arguments are appended in order (thread-safe) to the contents of the given sequence and its (sequence) length increases by the length of argument.
StringBuffer append(char[] str, int offset, int len) appends the string representation of a subarray of char array argument to the given sequence. The characters of the char array str, starting at index offset are appended to the contents of the given sequence and its (sequence) length increases by the value of length len.
Let's see the code examples for all four, but one by one.
Example: append(String str)
- public class AppendStringMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("1234");
- System.out.println("Output for append(String str):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.append("later..!!");
- System.out.println("String str1 after append: " + str1); //appends the string argument with string buffer
- System.out.println("Buffer is: " + str2);
- str2.append("-5678");
- System.out.println("String str2 after append: " + str2); //appends the string argument with string buffer
- }
- }
Output

Example: append(char c)
- public class AppendCharMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("1234");
- System.out.println("Output for append(char c):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.append('L');
- System.out.println("String str1 after append: " + str1); //appends char argument as string with string buffer
- System.out.println("Buffer is: " + str2);
- str2.append('%');
- System.out.println("String str2 after append: " + str2); //appends char argument as string with string buffer
- }
- }
Output

Example: append(char[] str)
- public class AppendCharStrMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Watch this ");
- StringBuffer str2 = new StringBuffer("123");
- System.out.println("Output for append(char[] str):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- char[] str = new char[]
- {
- 'M', 'e', 't', 'h', 'o', 'd'
- };
- str1.append(str);
- System.out.println("String str1 after append: " + str1);
- /*appends string representation of char array
- argument with string buffer*/
- System.out.println("Buffer is: " + str2);
- char[] strr = new char[]
- {
- '/', '3', '2', '1'
- };
- str2.append(strr);
- System.out.println("String str2 after append: " + str2);
- /*appends string representation of char array
- argument with string buffer*/
- }
- }
Output

Example: append(char[] str, int offset, int len)






Gowtham RajamanickamPosted May 19, 2015, 12:45 AM
good one
Gopi ChandPosted May 16, 2015, 4:22 AM
Thanks to everyone :)
Santhakumar MunuswamyPosted May 16, 2015, 3:47 AM
Thanks for nice article
NitinPosted May 16, 2015, 2:59 AM
good one
Sibeesh VenuPosted May 15, 2015, 7:52 AM
Good one.
Gopi ChandPosted May 15, 2015, 1:52 AM
Your appreciation is the thing that encourage me to write more and efficient.. :) Thanks to all c#corner members.
Abhishek JaiswalPosted May 15, 2015, 1:36 AM
Very well explained and nice presentation. Ebullient to see the next part! :)
Amit DiwanPosted May 15, 2015, 12:49 AM
The examples are really good with informative comments. Well done!