This blog will demonstrate utilizing and implementing the “append()” method with the “StringBuilder” and “StringBuffer” classes in Java.
What is StringBuilder and StringBuffer “append()” Method in Java?
The “append()” is a Java method of the “StringBuilder” and “StringBuffer” classes that appends the specified value as its(method) parameter to the corresponding sequence.
Syntax
In this syntax, “st” represents the “String”. However, integer, double, character, or boolean, etc can also be appended to the “StringBuilder” object by specifying it instead of “String” as the method’s parameter.
Example 1: Applying the “append()” Method With “StringBuilder” in Java
In this example, the “append()” method can be associated with the “StringBuilder” class to append the values comprising multiple data types to the created object:
public static void main(String[] args) {
StringBuilder object = new StringBuilder("Linuxhint");
String x = "Java Programming";
object.append(x);
System.out.println("The value after appending the string is: "+object);
object.append(x, 0, 4);
System.out.println("The value after appending the substring with indexing is: "+object);
int y = 10;
object.append(y);
System.out.println("The value after appending the integer is: "+object);
object.append("z");
System.out.println("The value after appending the character is: "+object);
object.append(2==3);
System.out.println("The boolean value based on the condition is: "+object);
}}
Apply the following steps in accordance with the above code:
- First of all, create a “StringBuilder” object named “object” using the “new” keyword and the “StringBuilder()” constructor, respectively.
- In the constructor parameter, specify the “String” value that needs to be appended with multiple data type values.
- In the next step, initialize a “String” value and append it to the created object via the associated “append()” method.
- After that, append the substring, i.e., “Java” in the declared string by specifying the start and end string indexes, respectively.
- Now, initialize an “Integer” value and likewise, append it to the “StringBuilder” object.
- Similarly, append the specified character to the object.
- Note: In this step, it can be observed that the value can be appended directly as well by specifying it as the method’s, i.e., “append()” parameter.
- Also, check for the provided condition and append the corresponding “boolean” value to the object.
- Lastly, display all the appended values on the console.
Output
In this output, it can be observed that each of the values corresponding to various data types is appended appropriately.
Example 2: Applying the “append()” Method With “StringBuffer” in Java
In this particular example, the “append()” method can be applied with the “StringBuffer” class to likewise append the values of multiple data types to the created object.
Syntax
In the above syntax, likewise, “st” represents the “String”. Also, the integer, double, character, or boolean, etc, values can be appended to the “StringBuffer” object by specifying it instead of “String” as the method’s parameter.
Let’s overview the below-provided example to understand the discussed concept:
public static void main(String[] args) {
StringBuffer object = new StringBuffer("Programming ");
String x = "in Java";
object.append(x);
System.out.println("The value after appending the string is: "+object);
object.append(x, 0, 2);
System.out.println("The value after appending the substring with indexing is: "+object);
int y = 10;
object.append(y);
System.out.println("The value after appending the integer is: "+object);
double z = 2.5;
object.append(z);
System.out.println("The value after appending the double is: "+object);
object.append("z");
System.out.println("The value after appending the character is: "+object);
object.append(4<5);
System.out.println("The boolean value based on the condition is: "+object);
}}
In the above code snippet, apply the following steps:
- Firstly, create a “StringBuffer” object via the discussed approach and place the stated string that needs to be appended with the values of various data types.
- Now, recall the discussed approaches for appending the string, substring, integer, character, and the outcome of the specified condition, respectively.
- Note that the “double” value appended here can also be appended in the case of the “StringBuilder” class by simply specifying it as the method’s, i.e., “append()” parameter.
- Finally, log the appended values comprising multiple data types on the console.
Output
In this outcome, it can be implied that the appended values are further appended upon invoking the method repeatedly with each of the data types.
Example 3: Applying the “Append()” Method With “StringBuilder” and “StringBuffer” Classes to Append Characters Array in Java
In this scenario, the discussed method can be implemented with both the “StringBuilder” and “StringBuffer” classes to append the characters from an array based on the method’s parameters.
Syntax (Character Array Scenario)
In the above-given syntax:
- “char[] st” refers to the characters that need to be appended.
- “int ofs” corresponds to the first character index to append.
- “int length” points to the number of characters that must be appended.
Now, let’s proceed to the below-provided example:
public static void main(String[] args) {
StringBuilder object1 = new StringBuilder("Linux ");
StringBuffer object2 = new StringBuffer("hint ");
System.out.println("The default value of StringBuilder is: " + object1);
System.out.println("The default value of StringBuffer is: " + object2);
char[] str = new char[]
{'J','a','v','a','P','r','o','g','r','a','m','m','i','n','g'};
object1.append(str, 0, 4);
System.out.println("The value after appending the characters "
+ "to StringBuilder is: " + object1);
object2.append(str, 0, 4);
System.out.println("The value after appending the characters "
+ "to StringBuffer is: " + object2);
}}
In the above lines of code, apply the following steps:
- First, create two objects named “object1” and “object2” of the “StringBuilder” and “StringBuffer” classes, respectively via the discussed approach.
- Also, display their default string values specified as the constructor’s parameter.
- Now, create an array of characters named “str” accumulating the stated characters.
- In the next step, append the characters to both of the created objects one by one based on the specified parameters.
- Algorithm: It works such that the characters will be appended starting from the initial index, i.e., “0” to a total of “4” characters sequentially, thereby excluding the index “4”.
- Finally, log the resultant appended object values in both the cases.
Output
This output signifies that the desired requirement is fulfilled.
Conclusion
The “append()” is a Java method of the “StringBuilder” and “StringBuffer” classes that appends the specified value to the current sequence. These values can be an integer, string, double, character, boolean, etc. This article discussed the implementation of the StringBuilder and StringBuffer “append()” method in Java.