How to Append Value to Java StringBuffer


1. Introdcution

In this example, we will learn how to append a value to StringBuffer in Java.

Source Code

package com.beginner.examples;

public class StringBufferAppend {

	public static void main(String[] args) {
		
		//Create StringBuffe.
		StringBuffer stringBuffer = new StringBuffer("Hello World");
		
		/*
		 * Using the method append(char c) appends character to StringBuffer.
		 */
		stringBuffer.append(" !");
		System.out.println("After appending , StringBuffer is : " + stringBuffer);
		
		/*
		 * Using the method append(char[] c) appends character to StringBuffer.
		 */
		char[] c = new char[] {' ','H','i',' '};
		stringBuffer.append(c);
		System.out.println("After appending , StringBuffer is : " + stringBuffer);
		
		/*
		 * Using the method append(int i) appends integer to StringBuffer.
		 */
		int i = 1024;
		stringBuffer.append(i);
		System.out.println("After appending , StringBuffer is : " + stringBuffer);
		
	}

}

Output:

After appending , StringBuffer is : Hello World !
After appending , StringBuffer is : Hello World ! Hi 
After appending , StringBuffer is : Hello World ! Hi 1024
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments