The classes in Java play a key role in applying multiple functionalities easily. The “StringBuilder” is one such class that contains the “append()” method. This method can be quite handy for programmers in the case of updating the data from time to time. For instance, while adding the bulk data to the already contained records regardless of data types or adding the selected data via indexing.
What is the StringBuilder append() Method in Java With Examples?
The StringBuilder “append()” method appends the string representation of the passed argument to the sequence.
Syntax
There are the following scenarios in which the “append()” method can be used to append the values to the StringBuilder sequence:
Syntax (Appending an Integer Value)
public StringBuilder append(int i)
In the given syntax, “i” represents the integer value to be appended.
Syntax (Appending a Double Value)
public StringBuilder append(double d)
Here, “d” is the double value that needs to be added to the sequence.
Syntax (Appending a Float Value)
public StringBuilder append(float f)
In this syntax, “f” corresponds to the float value to be added to the sequence.
Syntax (Appending a Long Value)
public StringBuilder append(long l)
Here, “l” implies the long value to be appended.
Syntax (Appending a String Value)
public StringBuilder append(String st)
Here, “st” is the string to be appended.
Syntax (Appending an Object Value)
public StringBuilder append(Object ob)
In the above syntax, “ob” is the object to be added to StringBuilder.
Syntax (Appending a Boolean Value)
public StringBuilder append(boolean x)
In this syntax, “x” points to the boolean value that is to be added to the sequence.
Syntax (Appending a Character Value)
public StringBuilder append(char b)
Here, “b” indicates the character value to be appended to the sequence.
Syntax (Appending a Character Array)
public StringBuilder append(char[] string)
In this syntax, “char[ ] string” implies the character array to be added to the sequence.
Syntax (Appending a Character Array in Interval)
public StringBuilder append(char[] string, int os, int length)
In the given syntax:
- “char[ ] string” is the char array.
- “os” refers to the first character’s index to be appended.
- “length” represents the characters to be appended.
Return Value
The method, in this case, returns an “IndexOutOfBoundsException” if the target index is out of range.
Syntax (Appending a CharSequence)
public StringBuilder append(CharSequence b)
Here, “b” is the CharSequence to be appended to the sequence.
Syntax (Appending a CharSequence in Interval)
public StringBuilder append(CharSequence b, int st, int ed)
In the above syntax:
- “b” is the CharSequence.
- “st” refers to the subsequence’s first index to be appended.
- “ed” indicates the subsequence’s last index that is to be appended.
Return Value
In this syntax, an “IndexOutOfBoundsException” is faced if the given index is out of range.
Syntax (Appending a StringBuilder Instance)
public StringBuilder append(StringBuilder stbuild)
In this syntax, “stbuild” represents the StringBuilder instance to be appended to another StringBuilder instance.
Syntax (Appending a CodePoint Value)
public StringBuilder appendCodePoint(int cp)
In the given syntax, “cp” is the code point as an integer value of the Unicode code point to be appended.
Method’s Return Value
The “append()” method gives a string object after the append process is completed.
Example 1: Applying the StringBuilder “append()” Method to Append the Integer Value
The below code example appends an integer to StringBuilder via the “append()” method:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
StringBuilder obj2 = new StringBuilder("This is Java!");
System.out.println("First Value Before Appending -> " + obj1);
System.out.println("Second Value Before Appending -> " + obj2);
int val = 88;
Integer val2 = new Integer(88);
obj1.append(val);
System.out.println("First Value After Appending -> " + obj1);
obj2.append(val2);
System.out.println("Second Value After Appending Integer Instance -> " + obj2);
}}
In this snippet of code:
- Create two StringBuilder instances using the “new” keyword and the
“StringBuilder()” constructor. - Note: The value as the constructor’s parameter is the value to be appended with the passed value.
- After that, initialize an integer value, and create an “Integer” instance.
- Now, use the “append()” method to append both the value and the integer’s instance to the StringBuilder sequence.
Output
In this output, it can be seen that both the integer and its instance are appended to their corresponding StringBuilder instances.
Example 2: Applying the StringBuilder “append()” Method to Append the Double Value
The code below uses the discussed method to append the double value as well as a double instance:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
StringBuilder obj2 = new StringBuilder("This is Java!");
System.out.println("First Value Before Appending -> " + obj1);
System.out.println("Second Value Before Appending -> " + obj2);
double val = 45654.79;
Double val2 = new Double(45654.79);
obj1.append(val);
System.out.println("First Value After Appending -> " + obj1);
obj2.append(val2);
System.out.println("Second Value After Appending Double Instance -> " + obj2);
}}
The code explanation is as follows:
- Create two StringBuilder instances to be appended.
- Now, initialize a double value and create a double instance, respectively.
- Apply the “append()” method to append both these double representations separately to the StringBuilder instances.
Output
From this output, it can be verified that the double value, as well as the double instance, are appended to their respective StringBuilder instances/sequences.
Example 3: Applying the StringBuilder “append()” Method to Append the Float Value
The “append()” method can also be used to append the float value, demonstrated below:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
StringBuilder obj2 = new StringBuilder("This is Java!");
System.out.println("First Value Before Appending -> " + obj1);
System.out.println("Second Value Before Appending -> " + obj2);
float val = 23.54f;
Float val2 = new Float(23.54);
obj1.append(val);
System.out.println("First Value After Appending -> " + obj1);
obj2.append(val2);
System.out.println("Second Value After Appending Float Instance -> " + obj2);
}}
In this code, recall the approach for creating StringBuilder instances. After that, apply the “append()” method twice to append both the defined float value and a Float instance, respectively.
Output
Example 4: Applying the StringBuilder “append()” Method to Append the Long Value
In this code example, a long value will be appended to the StringBuilder instance:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
StringBuilder obj2 = new StringBuilder("This is Java!");
System.out.println("First Value Before Appending -> " + obj1);
System.out.println("Second Value Before Appending -> " + obj2);
long val = 3233232;
Long val2 = new Long(3233232);
obj1.append(val);
System.out.println("First Value After Appending -> " + obj1);
obj2.append(val2);
System.out.println("Second Value After Appending Long Instance -> " + obj2);
}}
Here, first, the two instances of StringBuilder are created. After that, the “long” value is initialized and a “long” instance is created. Finally, the “append()” method appends both the long value representations to the StringBuilder sequence.
Output
Example 5: Applying the StringBuilder “append()” Method to Append the String Value
In this example, this method appends the string value as well as a string instance to the StringBuilder sequence:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Linux");
StringBuilder obj2 = new StringBuilder("This is Java");
System.out.println("First Value Before Appending -> " + obj1);
System.out.println("Second Value Before Appending -> " + obj2);
String val = "Shell";
String val2 = new String("Programming");
obj1.append(val);
System.out.println("First Value After Appending -> " + obj1);
obj2.append(val2);
System.out.println("Second Value After Appending String Instance -> " + obj2);
}}
Based on this code snippet, perform the following steps:
- Likewise, create two StringBuilder instances.
- Moving forward, initialize a string value as well as its instance, respectively.
- Now, use the “append()” method with both the string value and a string instance to append both these values with their corresponding sequences.
Output
As observed, the string value “Shell” and the instance value “Programming” are appended to the corresponding StringBuilder instances successfully.
Example 6: Applying the StringBuilder “append()” Method to Append the Object Value
In the below example, the discussed method appends an object value i.e., string, integer to the sequence:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("This is Linux");
System.out.println("Value Before Appending -> " + obj);
Object val = "Shell";
obj.append(val);
System.out.println("Value After Appending -> " + obj);
}}
Here, an object value is defined that is later appended to the StringBuilder sequence via the “append()” method.
Output
The “Object” type can also take an “int” type value to append/add to the StringBuilder instance, as follows:
Example 7: Applying the StringBuilder “append()” Method to Append the Boolean Value
This example uses the “append()” method to append both the possible boolean values to the StringBuilder:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
System.out.println("Value Before Appending -> " + obj1);
obj1.append(true);
System.out.println("Value After Appending -> " + obj1);
System.out.println();
StringBuilder obj2 = new StringBuilder("This is Java!");
System.out.println("Value Before Appending -> " + obj2);
obj2.append(false);
System.out.println("Value After Appending -> " + obj2);
}}
In this code, perform the following steps:
- Create a StringBuilder instance using the “new” keyword and the “StringBuilder()” constructor.
- Now, apply the “append()” method twice to append the boolean values “true” and “false”, respectively to the StringBuilder.
Output
This outcome verifies that the boolean values are appended to the StringBuilder appropriately.
Example 8: Applying the StringBuilder “append()” Method to Append a Character Value
The following code uses the “append()” method to append the character value to the sequence:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Java!");
System.out.println("Value Before Appending -> " + obj1);
obj1.append('a');
System.out.println("Value After Appending -> " + obj1);
}}
Here, likewise, create a StringBuilder instance and use the “append()” method to append the given character value to the sequence.
Output
As seen, the specified character as the “append()” method’s argument is appended accordingly.
Example 9: Applying the StringBuilder “append()” Method to Append the Character Array
In this demonstration, the discussed method appends an array of characters:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
System.out.println("Value Before Appending -> " + obj1);
char[] arrayChar = {'J', 'a', 'v', 'a'};
obj1.append(arrayChar);
System.out.println("Value After Appending -> " + obj1);
}}
In this code, append the entire defined character array named “arrayChar” to the StringBuilder instance via the applied “append()” method.
Output
The character array is appended as a single value to the string in the StringBuilder instance successfully.
However, to append a target character from a character’s array, the indexing can be done by pointing to the array, demonstrated below:
Example 10: Applying the StringBuilder “append()” Method to Append the Character Array in an Interval
This demonstration uses the “append()” method to append the array characters at given intervals:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry!");
System.out.println("Value Before Appending -> " + obj1);
char[] arrayChar = {'J', 'a', 'v', 'a'};
obj1.append(arrayChar, 1, 2);
System.out.println("Value After Appending -> " + obj1);
}}
Here, the “append()” method appends/adds the characters from the defined array to the StringBuilder in an interval such that 2 characters starting from the index “1” are appended.
Output
Example 11: Applying the StringBuilder “append()” Method to Append the CharSequence
A sequence of characters can also be appended using the “append()” method and the “CharSequence” sequence:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("This is Harry!");
System.out.println("Value Before Appending -> " + obj);
CharSequence sequence = "JavaProgramming";
obj.append(sequence);
System.out.println("Value After Appending -> " + obj);
}}
In this code, after creating the StringBuilder instance, specify a sequence of characters via the “CharSequence” sequence. Now, use the “append()” method to append this character’s sequence to StringBuilder.
Output
Example 12: Applying the StringBuilder “append()” Method to Append the CharSequence in an Interval
This particular example appends the CharSequence in a particular interval to the StringBuilder:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("This is Harry!");
System.out.println("Value Before Appending -> " + obj);
CharSequence sequence = "JavaProgramming";
obj.append(sequence, 2, 4);
System.out.println("Value After Appending -> " + obj);
}}
In this block of code, create a StringBuilder instance, specify the “CharSequence” sequence, and use the “append()” method to append the given CharSequence to the StringBuilder according to the specified interval.
Output
Here, it can be observed that the CharSequence sequence is appended according to the given interval.
Example 13: Applying the StringBuilder “append()” Method to Append the StringBuilder Instance
The following code appends a StringBuilder instance to another instance such that two StringBuilder instances are combined:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj1 = new StringBuilder("This is Harry");
System.out.println("Value Before Appending -> " + obj1);
StringBuilder obj2 = new StringBuilder("This is Java Programming");
obj1.append(obj2);
System.out.println("Instance After Appending the Other Instance-> " + obj1);
}}
In this example code, the second StringBuilder instance is appended to the first StringBuilder instance via the “append()” method.
Output
Bonus Example: Applying the “appendCodePoint()” Method to Append a Code Point
The “appendCodePoint()” method appends the code point argument’s string representation to this sequence. This method basically specifies the code point as an integer value and the corresponding modified string is returned:
package jbArticles;
public class Stringbuilderappend {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("This is Java");
System.out.println("Value Before Appending -> " + obj);
obj.appendCodePoint(64);
System.out.println("Value After Appending the Code Point -> " + obj);
}}
In this snippet of code, the “appendCodePoint()” method takes the code point as an integer, and the corresponding value i.e., “@” is appended to the sequence and returned as a string.
Output
As analyzed, the corresponding value with respect to the specified code point is appended to the StringBuilder sequence.
Conclusion
The StringBuilder “append()” method appends the string representation of the passed argument to the sequence. This method can be used to append the values of multiple types such as “boolean”, “int”, “float”, “double”, “char”, “string”, etc.
In addition to that, this method can be applied to append instances of these types, as well as an object, a character array, a “CharSequence”, and a “StringBuilder” instance.