Java – StringBuilder ensureCapacity() method

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

1. StringBuilder’s ensureCapacity(int) 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 StringBuilder capacity
  • Similarly, if specified minCapacity is non-positive then there will be no change in the StringBuilder 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 StringBuilder 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 non-positive integer

StringBuilderEnsureCapacityMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderEnsureCapacityMethod {

	public static void main(String[] args) {

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

		// 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 StringBuilder object with single space =>
		// capacity -> 16 + 1 = 17
		StringBuilder sb2 = new StringBuilder(" ");
		System.out.println("\n\n2.1 Capacity of "
				+ "StringBuilder(\" \") 		   : "
				+ sb2.capacity());

		// 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. StringBuilder object with initialized string =>
		// capacity -> 16 + 14 = 30
		StringBuilder sb3 = new StringBuilder("BenchResources");
		System.out.println("\n\n3.1 Capacity of "
				+ "StringBuilder(\"BenchResources\") : "
				+ sb3.capacity());

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

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

		// 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 StringBuilder() 		: 16
1.2 After ensuring more than current capacity   : 34

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

3.1 Capacity of StringBuilder("BenchResources") : 30
3.2 After ensuring less than current capacity   : 30

4.1 Capacity of StringBuilder(64)    : 64
4.2 After ensuring negative capacity : 64

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java - StringBuilder indexOf() method
Java - StringBuilder deleteCharAt() method