Learn StringBuffer() Class in Java: Lecture 2

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)
  1. public class AppendStringMethod   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Watch this ");  
  6.         StringBuffer str2 = new StringBuffer("1234");  
  7.         System.out.println("Output for append(String str):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         str1.append("later..!!");  
  11.         System.out.println("String str1 after append: " + str1); //appends the string argument with string buffer  
  12.         System.out.println("Buffer is: " + str2);  
  13.         str2.append("-5678");  
  14.         System.out.println("String str2 after append: " + str2); //appends the string argument with string buffer  
  15.     }  
Output
 
 
Example: append(char c)
  1. public class AppendCharMethod   
  2. {  
  3.     public static void main(String args[])  
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Watch this ");  
  6.         StringBuffer str2 = new StringBuffer("1234");  
  7.         System.out.println("Output for append(char c):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         str1.append('L');  
  11.         System.out.println("String str1 after append: " + str1); //appends char argument as string with string buffer  
  12.         System.out.println("Buffer is: " + str2);  
  13.         str2.append('%');  
  14.         System.out.println("String str2 after append: " + str2); //appends char argument as string with string buffer  
  15.     }  
  16. }  
Output
 
 
Example: append(char[] str)
  1. public class AppendCharStrMethod   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Watch this ");  
  6.         StringBuffer str2 = new StringBuffer("123");  
  7.         System.out.println("Output for append(char[] str):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         char[] str = new char[]   
  11.         {  
  12.             'M''e''t''h''o''d'  
  13.         };  
  14.         str1.append(str);  
  15.         System.out.println("String str1 after append: " + str1);  
  16.         /*appends string representation of char array 
  17.  argument with string buffer*/  
  18.         System.out.println("Buffer is: " + str2);  
  19.         char[] strr = new char[]   
  20.         {  
  21.             '/''3''2''1'  
  22.         };  
  23.         str2.append(strr);  
  24.         System.out.println("String str2 after append: " + str2);  
  25.         /*appends string representation of char array 
  26.  argument with string buffer*/  
  27.     }  
  28. }
Output
 
 
Example: append(char[] str, int offset, int len)
  1. public class AppendCharStrOffsetLengthMethod  
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Watch this ");  
  6.         StringBuffer str2 = new StringBuffer("123");  
  7.         System.out.println("Output for append(char[] str, int offset, int len):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         char[] str = new char[] {  
  11.             'S''e''l''e''c''t''i''o''n'  
  12.         };  
  13.         str1.append(str, 15);  
  14.         System.out.println("String str1 after append: " + str1);  
  15.         /*appends string representation of char array 
  16.  argument with string buffer with offset at index 1 and length as 5*/  
  17.         System.out.println("Buffer is: " + str2);  
  18.         char[] strr = new char[] {  
  19.             '*''4''5''6''7'  
  20.         };  
  21.         str2.append(strr, 03);  
  22.         System.out.println("String str2 after append: " + str2);  
  23.         /*appends string representation of char array 
  24.  argument with string buffer with offset at index 0 and length as 3*/  
  25.     }  
  26. }
Output
 
 
Now the next two methods will be discussed here.
 
 
StringBuffer append(CharSequence s) appends the specified character sequence to the given character sequence. The characters of charSequence argument are appended in order (thread-safe) and increase its (sequence) length by the length of the argument.
 
StringBuffer append(CharSequence s, int start, int end) appends the subsequence of the specified character sequence to the given sequence. The characters of the argument s, starting at index (start) are appended in order (thread-safe) to the content of the given sequence until the (end) index and the length of the sequence depends on the values of start and end index.
 
It also throws the exception indexOutOfBoundsException() if the index of the start or end is negative or the start index is greater than the end index or the end index is greater than the length of CharSequence.
 
Both methods return the reference to the given object.
 
Example: append(CharSequence s)
  1. public class AppendCharSqncMethod   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Come ");  
  6.         StringBuffer str2 = new StringBuffer("1A2B");  
  7.         System.out.println("Output for append(CharSequence s):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         CharSequence CS1 = "back";  
  11.         str1.append(CS1); //appends CharSqnc  
  12.         System.out.println("String str1 after append: " + str1); //return string buffer after appending  
  13.         System.out.println("Buffer is: " + str2);  
  14.         CharSequence CS2 = "=B2A1";  
  15.         str2.append(CS2); //appends Charsqnc  
  16.         System.out.println("String str2 after append: " + str2); //return string buffer after appending  
  17.     }  
  18. }
Output
Example: append(CharSequence s, int start, int end)
  1. public class AppendCharSqStartEndMethod  
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Come ");  
  6.         StringBuffer str2 = new StringBuffer("1A2B");  
  7.         System.out.println("Output for append(CharSequence s, int start, int end):");  
  8.         System.out.println();  
  9.         System.out.println("Buffer is: " + str1);  
  10.         CharSequence CS1 = "back soon";  
  11.         str1.append(CS1, 39);  
  12.         System.out.println("String str1 after append: " + str1); /*append the CharSqnc with start index 3 and end index 9*/  
  13.         System.out.println("Buffer is: " + str2);  
  14.         CharSequence CS2 = "=B2A1+345";  
  15.         str2.append(CS2, 58);  
  16.         System.out.println("String str2 after append: " + str2); /*append the CharSqnc with start index 5 and end index 8*/  
  17.     }  
  18. }
Output
 
 
Appending a new line to StringBuffer
 
Whenever we append content to the StringBuffer, the contents are appended to the end of the sequence without any spaces or line breaks between the contents.
 
Example
  1. public class AppendNewLine   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str = new StringBuffer("Come");  
  6.         str.append("again");  
  7.         str.append("later");  
  8.         str.append("..!!");  
  9.         System.out.println("Output is: " + str); //returns without space and line breaks  
  10.     }  
  11. }
Output
 
 
Now if we want to append spaces, then it would not be that much difficult to do that. Only we need to do is that: str.append(“”);
But if we want to append a new line, then it's a little bit difficult to do and there are many ways to do that. But most of them are platform-dependent, in other words, work for one platform like “\r\n” works for Windows but not for Unix (“\n” will work).
 
When implementing a StringBuffer, we have two platform-dependent ways to append a new line.
  1. Str.append(System.getProperty(“line.separator”));  
  2.                                  And  
  3. Str.append(System.lineSeparator()); (works on jdk 1.7 or higher) 
Let's see the code example for both ways.
 
Example
  1. public class AppendNewLineMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Have nice");  
  6.         System.out.println("Output for 1st way:");  
  7.         System.out.println();  
  8.         str1.append(System.getProperty("line.separator"));  
  9.         str1.append("time");  
  10.         System.out.println(str1);  
  11.         System.out.println();  
  12.         System.out.println("Output for 2nd way:");  
  13.         System.out.println();  
  14.         StringBuffer str2 = new StringBuffer("Good");  
  15.         str2.append(System.lineSeparator());  
  16.         str2.append("Luck");  
  17.         str2.append(System.lineSeparator());  
  18.         str2.append("Everyone");  
  19.         System.out.println(str2);  
  20.     }  
  21. }
Output
 
 
Click below link to switch to next lecture.
 
 
thank you, keep learning and sharing.