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

  • StringBuffer is a thread-safe, mutable sequence of characters.
    • StringBuffer can be modified at any point of time.
  • We will look into the length and capacity method of StringBuffer class.
  • StringBuffer provides the following methods to play with length & capacity.
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.
  • We will demonstrate the working of above methods with the help of examples.

Program – length & capacity method of StringBuffer class in java

  • Length method returns the number characters in a StringBuffer.
  • Capacity method returns the capacity of StringBuffer i.e. Internal storage available in a StringBuffer.
    1. E.g:  StringBuffer buffer = new StringBuffer()
    2. The length and capacity of buffer will be length = 0 and capacity = 16 (default initial capacity of StringBuffer).
    3. Now add contents to StringBuffer using buffer.append(“Yoga”)
    4. Length = 4, Capacity = 16
      • Length is 4 and Capacity will remain 16 as it is good enough to accommodate 4 characters.
      • Capacity will be increased, when we insert more elements to StringBuffer. (refer example code).
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

  • trimToSize method keep the capacity of StringBuffer as per input character sequence.
    • We have seen that the default capacity of StringBuffer is 16.
  • When we will use trimToSize method, the capacity will be as per the size of input buffer.
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

  • ensure capacity method pre-allocates the capacity of StringBuffer.
    1. e.g. In trailing example, we have set the capacity of StringBuffer to 40.
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
Scroll to Top