Java – StringBuffer ensureCapacity() method

In this article, we will discuss StringBuffer’s ensureCapacity() method which ensures capacity is at least equal to specified minimum capacity

1. StringBuffer’s ensureCapacity(int minCapacity) method:

  • This method ensures capacity is at least equal to specified minimum capacity
  • If current capacity is less than specified minCapacity, then new internal array will be allocated
  • After invoking ensureCapacity(), the new capacity will be larger than the specified minCapacity and it will be based on following calculation
  • Formula: New capacity = (old capacity * 2) + 2;
  • If specified minCapacity is less than current capacity then there will be no change in the StringBuffer capacity
  • Similarly, if specified minCapacity is non-positive then there will be no change in the StringBuffer capacity i.e.; no effect

1.1 Method Signature:

public void ensureCapacity(int minimumCapacity);

1.2 Returns:

  • This methods returns nothing, as it only ensures minimum capacity on the invoked StringBuffer object

2. Examples on ensureCapacity() method:

We will discuss all the below mentioned cases for ensureCapacity() method,

  1. Specified minimum capacity is greater than current capacity
  2. Specified minimum capacity is very much greater than current capacity
  3. Specified minimum capacity is lesser than current capacity
  4. Specified minimum capacity is negative integer

StringBufferEnsureCapacityMethod.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferEnsureCapacityMethod {

	public static void main(String[] args) {

		// 1. empty StringBuffer object =>
		// current capacity - default 16
		StringBuffer sb1 = new StringBuffer();
		System.out.println("1.1 Capacity of EMPTY "
				+ "StringBuffer()  : " + sb1.capacity());

		// 1.a after ensuring capacity =>
		// greater than current capacity
		sb1.ensureCapacity(20);
		System.out.println("1.2 After ensuring more than"
				+ " current capacity  : " + sb1.capacity());

		// 2. empty StringBuffer object with single space =>
		// capacity - 16 + 1 = 17
		StringBuffer sb2 = new StringBuffer(" ");
		System.out.println("\n\n2.1 Capacity of "
				+ "StringBuffer(\" \")     : " + sb2.capacity());

		// 2.a after ensuring capacity =>
		// much greater than current capacity
		sb2.ensureCapacity(65);
		System.out.println("2.2 After ensuring very high than"
				+ " current capacity  : " + sb2.capacity());

		// 3. StringBuffer object with initialized string =>
		// capacity - 16 + 14 = 30
		StringBuffer sb3 = new StringBuffer("BenchResources");
		System.out.println("\n\n3.1 Capacity of "
				+ "StringBuffer(\"Bench\") : " + sb3.capacity());

		// 3.a after ensuring capacity =>
		// lesser than current capacity
		sb2.ensureCapacity(25);
		System.out.println("3.2 After ensuring less than"
				+ " current capacity  : " + sb3.capacity());

		// 4. StringBuffer object with initial capacity 55
		StringBuffer sb4 = new StringBuffer(55);
		System.out.println("\n\n4.1 Capacity of "
				+ "StringBuffer(55)      : " + sb4.capacity());

		// 4.a after ensuring capacity => negative capacity
		sb2.ensureCapacity(-99);
		System.out.println("4.2 After ensuring negative"
				+ " capacity  : " + sb4.capacity());
	}
}

Output:

1.1 Capacity of EMPTY StringBuffer()  : 16
1.2 After ensuring more than current capacity  : 34

2.1 Capacity of StringBuffer(" ")     : 17
2.2 After ensuring very high than current capacity  : 65

3.1 Capacity of StringBuffer("Bench") : 30
3.2 After ensuring less than current capacity  : 30

4.1 Capacity of StringBuffer(55)      : 55
4.2 After ensuring negative capacity  : 55

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer indexOf() method
Java - StringBuffer deleteCharAt() method