String Buffer Example Java Program


StringBuffer class is a mutable class unlike the String class which is immutable. Both the capacity and character string of a StringBuffer Class. StringBuffer can be changed dynamically.


StringBuffer Variable_name = new StringBuffer("String_sequence");

String Buffer Example Program

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("She sells sea shells ");
		System.out.println("Given stringbuffer is: " + sb);
		System.out.println("length of stringbuffer is: " + sb.length() + ", capacity of stringbuffer is: " + sb.capacity());
		System.out.println("character at index 5 of the stringbuffer is: " + sb.charAt(5));
		System.out.println("codePointAt index 5 of the stringbuffer is: " + sb.codePointAt(5));
		System.out.println("appendind the stringbuffer: " + sb.append("on the sea shore"));
		System.out.println("substring of stringbuffer from index 10 to 20 is: " + sb.substring(10,20));
		System.out.println("reverse of the stringbuffer is: " + sb.reverse());

Sample Output

Output is:
Given stringbuffer is: She sells sea shells
length of stringbuffer is: 21, capacity of stringbuffer is: 37
character at index 5 of the stringbuffer is: e
codePointAt index 5 of the stringbuffer is: 101
appendind the stringbuffer: She sells sea shells on the sea shore
substring of stringbuffer from index 10 to 20 is: sea shells
reverse of the stringbuffer is: erohs aes eht no sllehs aes slles ehS