Java: StringBuffer append() method

In this article, we will discuss StringBuffer’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 to the buffer at the end

Note: There are 13 variants or overloaded append() method, those are,

 

Method Signature:

public StringBuffer append(boolean b);

public StringBuffer append(char c);

public StringBuffer append(int i);

public StringBuffer append(long lng);

public StringBuffer append(float f);

public StringBuffer append(double d);

public StringBuffer append(char[] str);

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

public StringBuffer append(CharSequence s);

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

public StringBuffer append(Object obj);

public StringBuffer append(String str);

public StringBuffer append(StringBuffer sb);

 

Returns:

  • Returns StringBuffer 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 StringBuffer object

Method signature:

public StringBuffer append(boolean b);

 

StringBufferAppendMethodForBoolean.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForBoolean {

	public static void main(String[] args) {

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

		// 1. appending boolean to StringBuffer using append(boolean) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(boolTest);

		// 2. appending boolean to StringBuffer using append(boolean) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(boolTest2);

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

Output:

1. Appending boolean TRUE to StringBuffer  : true
2. Appending boolean FALSE to StringBuffer : false

 

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

Method signature:

public StringBuffer append(int i);

 

StringBufferAppendMethodForInteger.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForInteger {

	public static void main(String[] args) {

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

		// 1. appending int value to StringBuffer using append(int) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(iTest1);

		// 2. appending int value to StringBuffer using append(int) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(iTest2);

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

Output:

1. Appending int value to StringBuffer : 19
2. Appending int value to StringBuffer : 333

 

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

Method signature:

public StringBuffer append(long lng);

 

StringBufferAppendMethodForLong.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForLong {

	public static void main(String[] args) {

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

		// 1. appending long value to StringBuffer using append(long) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(lngTest1);

		// 2. appending long value to StringBuffer using append(long) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(lngTest2);

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

Output:

1. Appending long value to StringBuffer : 1912345
2. Appending long value to StringBuffer : 333444999

 

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

Method signature:

public StringBuffer append(float f);

 

StringBufferAppendMethodForFloat.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForFloat {

	public static void main(String[] args) {

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

		// 1. appending float value to StringBuffer using append(float) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(ftTest1);

		// 2. appending float value to StringBuffer using append(float) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(ftTest2);

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

Output:

1. Appending float value to StringBuffer : 19.23456
2. Appending float value to StringBuffer : 258.58963

 

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

Method signature:

public StringBuffer append(double d);

 

StringBufferAppendMethodForDouble.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForDouble {

	public static void main(String[] args) {

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

		// 1. appending double value to StringBuffer using append(double) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(dblTest1);

		// 2. appending double value to StringBuffer using append(double) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(dblTest2);

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

Output:

1. Appending double value to StringBuffer : 19.23456
2. Appending double value to StringBuffer : 258.589635

 

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

Method signature:

public StringBuffer append(char c);

 

StringBufferAppendMethodForChar.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForChar {

	public static void main(String[] args) {

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

		// 1. appending char value to StringBuffer using append(char) method
		StringBuffer sb1 = new StringBuffer();
		sb1.append(chTest1);

		// 2. appending char value to StringBuffer using append(char) method
		StringBuffer sb2 = new StringBuffer();
		sb2.append(chTest2);

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

Output:

1. Appending char value to StringBuffer : A
2. Appending char value to StringBuffer : b

 

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

Method signature:

public StringBuffer append(char[] str);

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

 

StringBufferAppendMethodForCharArrays.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForCharArrays {

	public static void main(String[] args) {

		// define char[] variable
		char[] chArrayTest1 = {'B', 'e', 'n', 'c', 'h'};
		char[] chArrayTest2 = {'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

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

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

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

Output:

1. Appending char[] value to StringBuffer : Bench
2. Appending char[] value to StringBuffer : Resources

 

8. To append specified CharSequence into StringBuffer object

Method signature:

public StringBuffer append(CharSequence s);

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

 

StringBufferAppendMethodForCharSequence.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForCharSequence {

	public static void main(String[] args) {

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

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

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

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

Output:

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

 

9. To append specified String object into StringBuffer object

Method signature:

public StringBuffer append(Object obj);

public StringBuffer append(String str);

 

StringBufferAppendMethodForStringObject.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethodForStringObject {

	public static void main(String[] args) {

		// define String variable
		String str1 = "Resources.Net";
		String str2 = "Tutorial.tut";

		// 1. appending String value to StringBuffer using append(String) method
		StringBuffer sb1 = new StringBuffer("Bench");
		sb1.append(str1);

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

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

Output:

1. Appending String value to StringBuffer : BenchResources.Net
2. Appending String value to StringBuffer : OnlineTutorial.tut

 

10. To append specified one StringBuffer object into another StringBuffer object

Method signature:

public StringBuffer append(StringBuffer sb);

 

StringBufferAppendMethod.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferAppendMethod {

	public static void main(String[] args) {

		// 1. create StringBuffer object
		StringBuffer sb1 = new StringBuffer("First StringBuffer object");
		sb1.append(" created. ");

		// 2. create another StringBuffer object
		StringBuffer sb2 = new StringBuffer("Another StringBuffer object ");
		sb2.append("to be appended.");

		// appending StringBuffer value to another StringBuffer 
		// using append(StringBuffer) method
		sb1.append(sb2);

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

Output:

Appending 1-StringBuffer value to 2-StringBuffer : 

First StringBuffer object created. Another StringBuffer object to be appended.

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer capacity() method
Java: StringBuffer class