Categories
java

StringBuffer example in java

StringBuffer example in java

In Below example we will learn how to use StringBuffer example in java

StringBuffer is a thread safe, which means only one thread can at once
So It will be slow compared to StringBuilder.

Example

public class StringBufferExample {

	public static void main(String[] args) {
		
		//In below line we are defining the string buffer
		StringBuffer buffer = new StringBuffer();
		//.append is the keyword to concat the strings
		buffer.append("This");
		buffer.append(" is a ");
		buffer.append("String Buffer ");
		buffer.append("Example");
		
		System.out.println(buffer.toString());

	}

}

Output

This is a String Buffer Example

Reference

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

https://beginnersbug.com/difference-between-stringbuffer-and-stringbuilder-in-java/

Leave a Reply

Your email address will not be published. Required fields are marked *