Java: StringBuilder append() method

In this article, we will discuss StringBuilder’s append() method which internally converts respective data-type into String format first and then append to buffer at the very end

 

append() method:

  • This method is used to append given argument of any data-type at the end of buffer
  • Note: There are 13 variants or overloaded append() method, those are,

Method Signature:

public StringBuilder append(boolean b);

public StringBuilder append(char c)

public StringBuilder append(int i);

public StringBuilder append(long lng);

public StringBuilder append(float f);

public StringBuilder append(double d);

public StringBuilder append(char[] str);

public StringBuilder append(char[] str, int offset, int len);

public StringBuilder append(CharSequence s);

public StringBuilder append(CharSequence s, int start, int end);

public StringBuilder append(Object obj);

public StringBuilder append(String str);

public StringBuilder append(StringBuffer sb);

Returns:

  • Returns StringBuilder object after appending given argument at the end of the buffer
  • Argument can be any primitive data-types like boolean, char, int, long, float, double, etc.
  • It can also be used to append char[] array, CharSequence, Object or String and StringBuffer

 

Examples on append() method:

1. To append specified primitive boolean data-type into StringBuilder object

Method signature:

public StringBuilder append(boolean b);

StringBuilderAppendMethodForBoolean.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForBoolean {

	public static void main(String[] args) {

		// define boolean variable
		boolean boolTest1 = true;
		boolean boolTest2 = false;

		// 1. appending boolean to StringBuilder 
		// using append(boolean) method
		StringBuilder sb1 = new StringBuilder("This is ");
		sb1.append(boolTest1);

		// 2. appending boolean to StringBuilder 
		// using append(boolean) method
		StringBuilder sb2 = new StringBuilder("It is ");
		sb2.append(boolTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending boolean TRUE"
				+ " to StringBuilder  : " + sb1);
		System.out.println("2. Appending boolean FALSE"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending boolean TRUE to StringBuilder  : This is true
2. Appending boolean FALSE to StringBuilder : It is false

 

2. To append specified primitive int data-type into StringBuilder object

Method signature:

public StringBuilder append(int i);

StringBuilderAppendMethodForInteger.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForInteger {

	public static void main(String[] args) {

		// define int variable
		int iTest1 = 19;
		int iTest2 = 333;

		// 1. appending int value to StringBuilder 
		// using append(int) method
		StringBuilder sb1 = new StringBuilder(
				"Integer number is ");
		sb1.append(iTest1);

		// 2. appending int value to StringBuilder 
		// using append(int) method
		StringBuilder sb2 = new StringBuilder(
				"Other int number is ");
		sb2.append(iTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending int value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending int value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending int value to StringBuilder : Integer number is 19
2. Appending int value to StringBuilder : Other int number is 333

 

3. To append specified primitive long data-type into StringBuilder object

Method signature:

public StringBuilder append(long lng);

StringBuilderAppendMethodForLong.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForLong {

	public static void main(String[] args) {

		// define int variable
		long lngTest1 = 1912345;
		long lngTest2 = 333444999;

		// 1. appending long value to StringBuilder 
		// using append(long) method
		StringBuilder sb1 = new StringBuilder(
				"The appended long vale is ");
		sb1.append(lngTest1);

		// 2. appending long value to StringBuilder 
		// using append(long) method
		StringBuilder sb2 = new StringBuilder(
				"Other appended long vale is ");
		sb2.append(lngTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending long value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending long value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending long value to StringBuilder : 
	The appended long vale is 1912345
2. Appending long value to StringBuilder : 
	Other appended long vale is 333444999

 

4. To append specified primitive float data-type into StringBuilder object

Method signature:

public StringBuilder append(float f);

StringBuilderAppendMethodForFloat.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForFloat {

	public static void main(String[] args) {

		// define float variable
		float ftTest1 = 19.23456f;
		float ftTest2 = 258.589635f;

		// 1. appending float value to StringBuilder 
		// using append(float) method
		StringBuilder sb1 = new StringBuilder(
				"The float value is ");
		sb1.append(ftTest1);

		// 2. appending float value to StringBuilder 
		// using append(float) method
		StringBuilder sb2 = new StringBuilder(
				"Other float value is ");
		sb2.append(ftTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending float value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending float value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending float value to StringBuilder : 
	The float value is 19.23456
2. Appending float value to StringBuilder : 
	Other float value is 258.58963

 

5. To append specified primitive double data-type into StringBuilder object

Method signature:

public StringBuilder append(double d);

StringBuilderAppendMethodForDouble.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForDouble {

	public static void main(String[] args) {

		// define double variable
		double dblTest1 = 19.23456;
		double dblTest2 = 258.589635;

		// 1. appending double value to StringBuilder 
		// using append(double) method
		StringBuilder sb1 = new StringBuilder(
				"The double value is ");
		sb1.append(dblTest1);

		// 2. appending double value to StringBuilder 
		// using append(double) method
		StringBuilder sb2 = new StringBuilder(
				"Another double value is ");
		sb2.append(dblTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending double value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending double value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending double value to StringBuilder : 
	The double value is 19.23456
2. Appending double value to StringBuilder : 
	Another double value is 258.589635

 

6. To append specified primitive char data-type into StringBuilder object

Method signature:

public StringBuilder append(char c);

StringBuilderAppendMethodForChar.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForChar {

	public static void main(String[] args) {

		// define char variable
		char chTest1 = 'A';
		char chTest2 = 'b';

		// 1. appending char value to StringBuilder 
		// using append(char) method
		StringBuilder sb1 = new StringBuilder(
				"Upper-case char value is ");
		sb1.append(chTest1);

		// 2. appending char value to StringBuilder 
		// using append(char) method
		StringBuilder sb2 = new StringBuilder(
				"Lower-case char value is ");
		sb2.append(chTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending char value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending char value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending char value to StringBuilder : 
	Upper-case char value is A
2. Appending char value to StringBuilder : 
	Lower-case char value is b

 

7. To append specified char[] array into StringBuilder object

Method signature:

public StringBuilder append(char[] str);

public StringBuilder append(char[] str, int offset, int len);

StringBuilderAppendMethodForCharArray.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForCharArray {

	public static void main(String[] args) {

		// define char[] variable
		char[] chArrayTest1 = {'G', 'o', 'o', 'g', 'l', 'e'};
		char[] chArrayTest2 = {'O', 'r', 'a', 'c', 'l', 'e'};

		// 1. appending char[] value to StringBuilder 
		// using append(char[]) method
		StringBuilder sb1 = new StringBuilder();
		sb1.append(chArrayTest1);

		// 2. appending char[] value to StringBuilder 
		// using append(char[]) method
		StringBuilder sb2 = new StringBuilder();
		sb2.append(chArrayTest2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending char[] value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending char[] value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending char[] value to StringBuilder : Google
2. Appending char[] value to StringBuilder : Oracle

 

8. To append specified CharSequence into StringBuilder object

Method signature:

public StringBuilder append(CharSequence s);

public StringBuilder append(CharSequence s, int start, int end);

StringBuilderAppendMethodForCharSequence.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForCharSequence {

	public static void main(String[] args) {

		// define CharSequence variable
		CharSequence charSeq1 = "Bench";
		CharSequence charSeq2 = "Resources";

		// 1. appending CharSequence value to StringBuilder 
		// using append(CharSequence) method
		StringBuilder sb1 = new StringBuilder();
		sb1.append(charSeq1);

		// 2. appending CharSequence value to StringBuilder 
		// using append(CharSequence) method
		StringBuilder sb2 = new StringBuilder();
		sb2.append(charSeq2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending CharSequence value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending CharSequence value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending CharSequence value to StringBuilder : Bench
2. Appending CharSequence value to StringBuilder : Resources

 

9. To append specified String object into StringBuilder object

Method signature:

public StringBuilder append(Object obj);

public StringBuilder append(String str);

StringBuilderAppendMethodForStringObject.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethodForStringObject {

	public static void main(String[] args) {

		// define String variable
		String str1 = "Michigan";
		String str2 = "University";

		// 1. appending String value to StringBuilder 
		// using append(String) method
		StringBuilder sb1 = new StringBuilder("University of ");
		sb1.append(str1);

		// 2. appending String value to StringBuilder 
		// using append(String) method
		StringBuilder sb2 = new StringBuilder("Mumbai ");
		sb2.append(str2);

		// print to console - internally invokes toString() method
		System.out.println("1. Appending String value"
				+ " to StringBuilder : " + sb1);
		System.out.println("2. Appending String value"
				+ " to StringBuilder : " + sb2);
	}
}

Output:

1. Appending String value to StringBuilder : University of Michigan
2. Appending String value to StringBuilder : Mumbai University

 

10. To append specified StringBuffer object into StringBuilder object

Method signature:

public StringBuilder append(StringBuffer sb);

StringBuilderAppendMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderAppendMethod {

	public static void main(String[] args) {

		// create StringBuilder object
		StringBuilder sbuilder = new StringBuilder(
				"First StringBuilder object");
		sbuilder.append(" created. ");

		// create StringBuffer object
		StringBuffer sbuffer = new StringBuffer(
				"StringBuilder object ");
		sbuffer.append("appended to the StringBuiider object.");

		// appending StringBuffer to StringBuilder 
		// using append(StringBuffer) method
		sbuilder.append(sbuffer);

		// print to console - internally invokes toString() method
		System.out.println("Appending StringBuffer value"
				+ " to StringBuilder : \n\n"
				+ sbuilder);
	}
}

Output:

Appending StringBuffer value to StringBuilder : 

First StringBuilder object created. 
StringBuilder object appended to the StringBuiider object.

 

References:

 

Happy Coding !!
Happy Learning !!

Java: StringBuilder capacity() method
Java: StringBuilder class