Java StringBuilderappend 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.

appendboolean 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 appendboolean 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

appendchar 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 appendchar 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

appendchar 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 appendchar 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

appenddouble 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 appenddouble 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

appendfloat 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 appendfloat 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

appendint 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 appendint 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

appendlong 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 appendlong 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

appendCharSequence 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 appendCharSequence 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

appendObject 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 appendObject 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]

appendString 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 appendString 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

appendchar 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 appendchar 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

appendCharSequence 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 appendCharSequence 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

appendStringBuffer 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 appendStringBuffer 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.