String Buffer and its Method

Creating an Immutable Class in JAVA

Java provides us the facility to create our own immutable class just like String class. In such a class, if we create its object then we cannot perform any change in its object. And if we try to perform any changes then a new object will be created with those changes. Due to our run time method call if there are no changes in the content of object then existing object will only be returned.
Consider the example of a custom immutable class:

public final class Mall
{
    //initializing data members
    private int storeid;
    //defining constructor
    Mall(int storeid)
    {
        this.storeid = storeid;
    }
    public Mall modify(int storeid)
    {
        if(this.storeid == storeid)
        {
            return this;
        }
        return (new Mall(storeid));
    }
    public static void main(String []args)
    {
        Mall m1 = new Mall(1001);
        Mall m2 = new Mall(1002);
        Mall m3 = new Mall(1001);
        System.out.println(m1 == m2);    //comparing 1st and 2nd object
        System.out.println(m1 == m3);    //comparing 1st and 3rd object
    }
}

Output of the above code will be

false
true

StringBuffer Class

StringBuffer class is present in java.lang package. It is recommended to use StringBuffer class if the content or strings are changing frequently. It is because for every change in content, a new object will be created and take memory.

Constructors of StringBuffer class:

1) StringBuffer():

This is a default constructor of StringBuffer class. This constructor creates an empty object of StringBuffer class with default initial capacity. Once the content reaches the maximum capacity, a new StringBuffer object gets created dynamically with new capacity of (current_capacity + 1) *2.
For example:

StringBuffer sb1 = new StringBuffer();
System.out.println(sb1.capacity());
sb1.append("abcdefghijklmnop");
System.out.println(sb1.capacity());
sb1.append("q");
System.out.println(sb1.capacity());

Its output will be:

1/16
16
34

2) StringBuffer(int initial_capacity):

This constructor creates an empty object with a specified initial capacity. For example:

StringBuffer sb1 = new StringBuffer(20);
System.out.println(sb1.capacity());
//output will be 20

3) StringBuffer(String str):

This constructor creates an object with specified string as its content and capacity = 16 + str.length(). For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.capacity());
//output will be 21

Important Methods of StringBuffer class

1) length(): This method returns the length of the string stored in object of StringBuffer class. For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.length());
//output will be 5

2) capacity(): This method returns the capacity of the object of StringBuffer class. For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.capacity());
//output will be 21

3) chatAt(): This method returns the character present at the specified index. For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.charAt(2));
//output will be "m"

4) setCharAt(): This method sets the specified character at the specified location in the string. For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.setCharAr(2,"M"));
//output will be "JaMes"

5) append(): This method appends or adds the given string, int, float, double, boolean or any other such values in the end of the content present in the object. For example:

StringBuffer sb1 = new StringBuffer("Hello");
sb1.append(true);
sb1.append(3);
sb1.append(0.14);
sb1.append("Bye");
System.out.println(sb1);
//output will be "Hellotrue30.14Bye"

6) insert(): This method inserts any other value or object at the specified index. For example:

StringBuffer sb1 = new StringBuffer("Leaders");
System.out.println(sb1.insert(6,"ship"));
//output will be "Leadership"

7) delete(): This method deletes any characters present between given begin index and end-1 index. For example:

StringBuffer sb1 = new StringBuffer("Leaders");
System.out.println(sb1.delete(4,7));
//output will be "Lead"

8) deleteCharAt(): This method deletes the character present at given index. For example:

StringBuffer sb1 = new StringBuffer("Leaders");
System.out.println(sb1.deleteCharAt(6));
//output will be "Leader"

9) reverse(): This method reverses the content present in the object. For example:

StringBuffer sb1 = new StringBuffer("Leaders");
System.out.println(sb1.reverse());
//output will be "sredaeL"

10) setLength(): This method sets the length of the string that can be stored in the object. If the length of the given string exceeds then it will be trimmed to the set length. For example:

StringBuffer sb1 = new StringBuffer();
sb1.setLength(3);
sb1.append("Leaders");
System.out.println(sb1);
//output will be "Lea"

11) ensureCapacity(): This method set the capacity of the object. For example:

StringBuffer sb1 = new StringBuffer("James");
System.out.println(sb1.capacity());
//output will be 21
sb1.ensureCapacity(2000); 
//output will be 2000

12) trimToSize(): This method releases the extra memory that is allocated to the object. After calling this method, length and capacity of the object will be equal. For example:

StringBuffer sb1 = new StringBuffer();
sb1.ensureCapacity(2000);
sb1.append("James");
sb1.trimToSize();
System.out.println(sb1.capacity());
//output will be "5"

This is all about the stringBuffer class in Java and its methods.

Leave a Comment

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