Java StringBuilder ensureCapacity() Method Example

Java StringBuilder ensureCapacity(int minimumCapacity) method ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.

Java StringBuilder ensureCapacity() Method Example

Example to ensure the capacity of string buffer using ensureCapacity() method.
public class EnsureCapacityExample {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();


Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course