Site icon

Difference b/w length & capacity method (impact of trim) of StringBuffer in java

public int	length()
Returns the length (character count).

public int capacity()
Returns the current capacity. The capacity is the amount of storage available.

public void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum. 

public void trimToSize()
Attempts to reduce storage used for the character sequence.

Program – length & capacity method of StringBuffer class in java

private static void DemoLengthAndCapacity() {
	StringBuffer buffer = new StringBuffer();
	length = buffer.length();
	capacity = buffer.capacity();
	//Content = , Length = 0, Capacity = 16
	System.out.printf("1. Content = %s, Length = %d, Capacity = %d", buffer,length,capacity);

	//Append first string
	buffer.append("21st June ");
	length = buffer.length();
	capacity = buffer.capacity();
	//Output:  Content = 21st June , Length = 10, Capacity = 16
	System.out.printf("\n2. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);

	//Append Second string
	buffer.append("Yoga ");
	length = buffer.length();
	capacity = buffer.capacity();
	//Output: Content = 21st June Yoga , Length = 15, Capacity = 16
	System.out.printf("\n3. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);

	//Length of String overshoot and capacity will be increased
	buffer.append("Day");
	length = buffer.length();
	capacity = buffer.capacity();
	//Output: Content = 21st June Yoga Day, Length = 18, Capacity = 34
	System.out.printf("\n4. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
}

Output – length & capacity method of StringBuffer class in java

1. Content = , Length = 0, Capacity = 16
2. Content = 21st June , Length = 10, Capacity = 16
3. Content = 21st June Yoga , Length = 15, Capacity = 16
4. Content = 21st June Yoga Day, Length = 18, Capacity = 34

Program – trim to size method of StringBuffer class in java

private static void DemoTrimToSize() {

	StringBuffer buffer = new StringBuffer("Yoga Day");
	//Trim to size
	buffer.trimToSize();
	int length = buffer.length();
	int capacity = buffer.capacity();
	//Output: Content = Yoga Day, Length = 8, Capacity = 8
	System.out.printf("\n5. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
}

Output – trim to size method of StringBuffer class in java

Content = Yoga Day, Length = 8, Capacity = 8

Program – ensure capacity method of StringBuffer class in java

private static void DemoEnsureCapacity() {
	StringBuffer buffer = new StringBuffer();
	buffer.ensureCapacity(40);
	buffer.append("Yoga");

	int length = buffer.length();
	int capacity = buffer.capacity();
	//Output: Content = Yoga, Length = 4, Capacity = 40
	System.out.printf("\n6. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
}

Output – ensure capacity method of StringBuffer class in java

Content = Yoga, Length = 4, Capacity = 40

Complete code of length,capacity, trimToSize & ensureCapacity method of StringBuffer

package org.learn;

public class StringBuffLenCap {

    private static int length = 0;
    private static int capacity = 0;
    public static void main(String[] args) {

        //Buffer is empty.
        DemoLengthAndCapacity();
        DemoTrimToSize();
        DemoEnsureCapacity();
    }

    private static void DemoLengthAndCapacity() {
        StringBuffer buffer = new StringBuffer();
        length = buffer.length();
        capacity = buffer.capacity();
        //Content = , Length = 0, Capacity = 16
        System.out.printf("1. Content = %s, Length = %d, Capacity = %d", buffer, length,capacity);

        //Append first string
        buffer.append("21st June ");
        length = buffer.length();
        capacity = buffer.capacity();
        //Output:  Content = 21st June , Length = 10, Capacity = 16
        System.out.printf("\n2. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);

        //Append Second string
        buffer.append("Yoga ");
        length = buffer.length();
        capacity = buffer.capacity();
        //Output: Content = 21st June Yoga , Length = 15, Capacity = 16
        System.out.printf("\n3. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);

        //Length of String overshoot and capacity will be increased
        buffer.append("Day");
        length = buffer.length();
        capacity = buffer.capacity();
        //Output: Content = 21st June Yoga Day, Length = 18, Capacity = 34
        System.out.printf("\n4. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
    }

    private static void DemoTrimToSize() {
        StringBuffer buffer = new StringBuffer("Yoga Day");
        //Trim to size
        buffer.trimToSize();
        int length = buffer.length();
        int capacity = buffer.capacity();
        //Output: Content = Yoga Day, Length = 8, Capacity = 8
        System.out.printf("\n5. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
    }

    private static void DemoEnsureCapacity() {
        StringBuffer buffer = new StringBuffer();
        buffer.ensureCapacity(40);
        buffer.append("Yoga");

        int length = buffer.length();
        int capacity = buffer.capacity();
        //Output: Content = Yoga, Length = 4, Capacity = 40
        System.out.printf("\n6. Content = %s, Length = %d, Capacity = %d",buffer,length,capacity);
    }
}

Output of length,capacity, trimToSize & ensureCapacity methods

1. Content = , Length = 0, Capacity = 16
2. Content = 21st June , Length = 10, Capacity = 16
3. Content = 21st June Yoga , Length = 15, Capacity = 16
4. Content = 21st June Yoga Day, Length = 18, Capacity = 34
5. Content = Yoga Day, Length = 8, Capacity = 8
6. Content = Yoga, Length = 4, Capacity = 40
Exit mobile version