String Builder Example Java Program


To avoid unnecessary memory usage, StringBuilder class is used. It provides similar functionality to Strings, but stores its data in a mutable way. Only one StringBuilder object is created. Also because object creation is time consuming, using StringBuilder produces much faster code.


StringBuilder Variable_name=new StringBuilder("String_sequence");

String Builder Example Program

public class StringBuilderExample {
    public static void main(String[] args) {
        String str = "She sells sea shells on the sea shore";
		System.out.println("The input string is: "+str);
        int len = str.length();
        char[] arr1 = new char[len];
        char[] arr2 = new char[len];
        for (int i = 0; i < len; i++) {
            arr1[i] = 
        for (int j = 0; j < len; j++) {
            arr2[j] =
                arr1[len - 1 - j];
        String revstr =
            new String(arr2);
        System.out.println("After operation, the input string becomes: "+revstr);

Sample Output

Output is:
The input string is: She sells sea shells on the sea shore
After operation, the input string becomes: erohs aes eht no sllehs aes slles ehS