Categories
java

StringBuilder in java with example

StringBuilder in java with example

In this tutorial we will learn StringBuilder in java with example.

StringBuilder in java is widely used to concat two strings 
It is non synchronized (Not thread safe)

Syntax

sb.append("")

Example

public class StringBuilderExample {

	public static void main(String[] args) {
		StringBuilder builder = new StringBuilder();
		// .append method used to concat string
		builder.append("This");
		builder.append(" is");
		builder.append(" an");
		builder.append(" example");

		// printing the string
		System.out.println(builder.toString());

	}
}

Output

This is an example
Reference

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.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 *