Java StringBuilder.append() – Examples
In this tutorial, we will learn about the Java StringBuilder.append() function, and learn how to use this function to append String representation of different types of values and object to the sequence in StringBuilder, with the help of examples.
append(boolean b)
StringBuilder.append() appends the string representation of the boolean argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(boolean b)
where
Parameter | Description |
---|---|
b | Boolean value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 1 – append(boolean b)
In this example, we will create a StringBuilder with some initial string literal, and a boolean variable with an initial value. We will then use append() method to append the boolean value to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// Appending the boolean value
boolean b = true;
stringBuilder.append(b);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abctrue
append(char c)
StringBuilder.append() appends the string representation of the char argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(char c)
where
Parameter | Description |
---|---|
c | Char value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 2 – append(char c)
In this example, we will create a StringBuilder with some initial string literal, and a char variable with an initial value. We will then use append() method to append the character to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append char
char ch = 's';
stringBuilder.append(ch);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abcs
append(char[] str)
StringBuilder.append() appends the string representation of the char array argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(char[] str)
where
Parameter | Description |
---|---|
str | Char Array that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 3 – append(char[] str)
In this example, we will create a StringBuilder with some initial string literal, and a character array with an initial value. We will then use append() method to append the character array to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append char array
char[] str = {'d', 'e', 'f'};
stringBuilder.append(str);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abcdef
append(double d)
StringBuilder.append() appends the string representation of the double argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(double d)
where
Parameter | Description |
---|---|
d | Double value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 4 – append(double d)
In this example, we will create a StringBuilder with some initial string literal, and a double variable with an initial value. We will then use append() method to append the double to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append double
double d = 321.14;
stringBuilder.append(d);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abc321.14
append(float f)
StringBuilder.append() appends the string representation of the float argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(float f)
where
Parameter | Description |
---|---|
f | Float value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 5 – append(float f)
In this example, we will create a StringBuilder with some initial string literal, and a float variable with an initial value. We will then use append() method to append the float to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append float
float f = 3.14f;
stringBuilder.append(f);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abc3.14
append(int i)
StringBuilder.append() appends the string representation of the int argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(int i)
where
Parameter | Description |
---|---|
i | Integer value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 6 – append(int i)
In this example, we will create a StringBuilder with some initial string literal, and a integer variable with an initial value. We will then use append() method to append the integer to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append integer
int i = 25;
stringBuilder.append(i);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abc25
append(long l)
StringBuilder.append() appends the string representation of the long argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(long l)
where
Parameter | Description |
---|---|
l | Long value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 7 – append(long l)
In this example, we will create a StringBuilder with some initial string literal, and a long variable with an initial value. We will then use append() method to append the long value to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append long value
long l = 56324;
stringBuilder.append(l);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Original value : abc
After appending : abc56324
append(CharSequence s)
StringBuilder.append() appends the specified character sequence to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(CharSequence s)
where
Parameter | Description |
---|---|
s | Character Sequence that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 8 – append(CharSequence s)
In this example, we will create a StringBuilder with some initial string literal, and a CharSequence object with an initial value. We will then use append() method to append the sequence in CharSequence object to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append character sequence
CharSequence s = "def";
stringBuilder.append(s);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Original value : abc
After appending : abcdef
append(Object obj)
StringBuilder.append() appends the string representation of the Object argument to the existing sequence in this StringBuilder object. The returned value of Object.toString() shall be appended to StringBuilder.
Syntax
The syntax of append() function is
append(Object obj)
where
Parameter | Description |
---|---|
obj | Object, whose return value of toString() method, that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 9 – append(Object obj)
In this example, we will create a StringBuilder with some initial string literal, and an object obj
of class Person. We will then use append() method to append the return value of Person.toString() to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append object
Person obj = new Person("XYZ", 22);
stringBuilder.append(obj);
System.out.println("After appending : " + stringBuilder);
}
}
class Person{
public String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return '[' + name + '-' + age + ']';
}
}
Output
Original value : abc
After appending : abc[XYZ-22]
append(String str)
StringBuilder.append() appends the specified string to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(String str)
where
Parameter | Description |
---|---|
str | String value that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this java.lang.AbstractStringBuilder object.
Example 10 – append(String str)
In this example, we will create a StringBuilder with some initial string literal, and a String variable with an initial value. We will then use append() method to append the String value to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append string
String str = "def";
stringBuilder.append(str);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Original value : abc
After appending : abcdef
append(char[] str, int offset, int len)
StringBuilder.append() appends the string representation of a subarray of the char array argument to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(char[] str, int offset, int len)
where
Parameter | Description |
---|---|
str | Character Array value that shall be appended to the sequence in StringBuilder. |
offset | The position in str from which the sub array is considered for appending to StringBuilder. |
len | The length of sub array from offset that is considered for appending to StringBuilder. |
Returns
The function returns a reference to this StringBuilder object.
Example 11 – append(char[] str, int offset, int len)
In this example, we will create a StringBuilder with some initial string literal, and a character array variable with an initial value. We also define an offset and length of sub-array in the char array variable we initialized. We will then use append() method to append the sub-array to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append char array with offset and length
char[] str = {'d', 'e', 'f', 'g', 'h', 'i'};
int offset = 2;
int len = 3;
stringBuilder.append(str, offset, len);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Oringal value : abc
After appending : abcfgh
append(CharSequence s, int start, int end)
StringBuilder.append() appends a subsequence of the specified CharSequence to the existing sequence in this StringBuilder object.
Syntax
The syntax of append() function is
append(CharSequence s, int start, int end)
where
Parameter | Description |
---|---|
s | Character Sequence that shall be appended to the sequence in StringBuilder. |
start | Start position of the subsequence in s . |
end | End position of the subsequence in s . |
Returns
The function returns a reference to this StringBuilder object.
Example 12 – append(CharSequence s, int start, int end)
In this example, we will create a StringBuilder with some initial string literal, and a CharSequence variable with an initial value. We also define an start and end of sub-sequence in the CharSequence object we initialized. We will then use append() method to append the sub-sequence to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append char sequence from start to end
CharSequence s = "defghijk";
int start = 2;
int end = 5;
stringBuilder.append(s, start, end);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Original value : abc
After appending : abcfgh
append(StringBuffer sb)
In this example, we will create a StringBuilder with some initial string literal, and a String variable with an initial value. We will then use append() method to append the String value to the StringBuilder.
Syntax
The syntax of append() function is
append(StringBuffer sb)
where
Parameter | Description |
---|---|
sb | String Buffer that shall be appended to the sequence in StringBuilder. |
Returns
The function returns a reference to this StringBuilder object.
Example 13 – append(StringBuffer sb)
In this example, we will create a StringBuilder with some initial string literal, and a StringBuffer object with an initial value. We will then use append() method to append the StringBuffer value to the StringBuilder.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abc");
System.out.println("Original value : " + stringBuilder);
// append string buffer
StringBuffer sb = new StringBuffer("def");
stringBuilder.append(sb);
System.out.println("After appending : " + stringBuilder);
}
}
Output
Original value : abc
After appending : abcdef
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.append() function, and also learnt how to use this function with the help of examples.