StringBuilder class

StringBuilder class


Posted in : Java Posted on : April 19, 2012 at 6:40 PM Comments : [ 0 ]

In this tutorial you will learn about Java StringBuilder class

StringBuilder class

java java.lang.StringBuilder class added in jdk 1.5. It implements Serializable, CharSequence and Appendable interfaces. This class is also a mutable class which contains the mutable sequence of characters. It is similar to the StringBuffer class class but the difference is that StringBuilder is not thread save and its methods are not synchronized. The multiple threads can access the instances of StringBuilder class. It is recommended that to use StringBuilder class can be used to achieve faster execution rather than StringBuffer class under most of the implementation.

Following is an example of StringBuilder class

StringBuilderExample.java

package net.roseindia;

public class StringBuilderExample {
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder("Java");

		/* append() */
		stringBuilder.append(" Pro");
		System.out.println(stringBuilder);

		/* appendCodePoint() */
		stringBuilder.appendCodePoint(5);
		System.out.println(stringBuilder);

		/* capacity() */
		int capacity = stringBuilder.capacity();
		System.out.println(capacity);

		/* charAt() */
		char c = stringBuilder.charAt(2);
		System.out.println(c);

		/* codePointAt() */
		int cp = stringBuilder.codePointAt(5);
		System.out.println(cp);

		/* codePointBefore() */
		int cpBefore = stringBuilder.codePointBefore(5);
		System.out.println(cpBefore);

		/* codePointCount() */
		int cpcount = stringBuilder.codePointCount(0, 2);
		System.out.println(cpcount);

		/* delete() */
		stringBuilder.delete(4, 8);
		System.out.println(stringBuilder);

		/* deleteCharAt() */
		stringBuilder.deleteCharAt(4);
		System.out.println(stringBuilder);

		/* indexOf() */
		int index = stringBuilder.indexOf("a");
		System.out.println(index);

		/* indexOf() */
		stringBuilder.insert(4, " Programming");
		System.out.println(stringBuilder);

		/* length() */
		int length = stringBuilder.length();
		System.out.println(length);

		/* replace() */
		stringBuilder.replace(4, 16, " Program");
		System.out.println(stringBuilder);

		/* substring() */
		String newString = stringBuilder.substring(5);
		System.out.println(newString);

		/* subSequence() */
		CharSequence charSequence = stringBuilder.subSequence(0, 4);
		System.out.println(charSequence);

		/* reverse() */
		stringBuilder.reverse();
		System.out.println(stringBuilder);
	}
}


When you run this application it will display message as shown below:


Pro
Java Pro
20
v
80
32
2
Java
Java
1
Java Programming
16
Java Program
Program
Java
margorP avaJ

Download Select Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics