Open In App

Java ensureCapacity(int minimumCapacity) Method

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the ensureCapacity(int minimumCapacity) method of the StringBuilder and StringBuffer classes is used to ensure that the buffer has a minimum capacity. If the current capacity is less than the specified minimum, it will increase the capacity to accommodate the required size.

Example 1: The below Java program demonstrates how the ensureCapacity() method makes sure there's enough space to hold a certain number of characters.

Java
// Java program to demonstrate ensureCapacity() method
public class Geeks {

    public static void main(String[] args)
    {

        // Create a StringBuilder object
        StringBuilder sb = new StringBuilder();

        // Display the default capacity
        System.out.println("Default Capacity: "
                           + sb.capacity());

        // Ensure the capacity for 50 characters
        sb.ensureCapacity(50);
        System.out.println(
            "Capacity after ensuring space for 50 characters: "
            + sb.capacity());
    }
}

Output
Default Capacity: 16
Capacity after ensuring space for 50 characters: 50

Syntax of ensureCapacity() Method

void ensureCapacity(int minimumCapacity)

  • Parameter: minimumCapacity: The minimum capacity that the buffer should have.
  • Return Type: This method does not return any value; it modifies the capacity of the buffer.

Example 2: The below Java program demonstrates how the ensureCapacity() method increases the buffer capacity to ensure it can hold at least 30 characters.

Java
// Java program to demonstrate ensureCapacity()
//  increasing capacity
public class Geeks {

    public static void main(String[] args)
    {

        // Create a StringBuilder object
        StringBuilder sb = new StringBuilder();

        // Display the default capacity
        System.out.println("Default Capacity: "
                           + sb.capacity());

        // Ensure the capacity for 30 characters
        sb.ensureCapacity(30);
        System.out.println(
            "Capacity after ensuring space for 30 characters: "
            + sb.capacity());
    }
}

Output
Default Capacity: 16
Capacity after ensuring space for 30 characters: 34

Example 3: The below Java program demonstrates how the ensureCapacity() ensures the buffer capacity is large enough to hold at least 100 characters.

Java
// Java program to demonstrate ensureCapacity()
//  with larger size
public class Geeks {

    public static void main(String[] args)
    {

        // Create a StringBuilder object
        StringBuilder sb = new StringBuilder();

        // Ensure the capacity for 100 characters
        sb.ensureCapacity(100);
        System.out.println(
            "Capacity after ensuring space for 100 characters: "
            + sb.capacity());
    }
}

Output
Capacity after ensuring space for 100 characters: 100

Example 4: The below Java program demonstrates how the ensureCapacity() ensure sufficient capacity for 50 characters while maintaining the existing content.

Java
// Java program to demonstrate ensureCapacity() with content
public class Geeks {

    public static void main(String[] args)
    {

        // Create a StringBuilder object
        StringBuilder sb = new StringBuilder("Hello");

        // Display current capacity
        System.out.println("Current Capacity: "
                           + sb.capacity());

        // Ensure the capacity for 50 characters
        sb.ensureCapacity(50);
        System.out.println(
            "Capacity after ensuring space for 50 characters: "
            + sb.capacity());
    }
}

Output
Current Capacity: 21
Capacity after ensuring space for 50 characters: 50

Example 5: The below Java program demonstrates how the ensureCapacity() ensure the buffer has enough capacity to hold at least 100 characters.

Java
// Java program to demonstrate ensureCapacity()
//  with StringBuffer
public class Geeks {

    public static void main(String[] args)
    {

        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer();

        // Display current capacity
        System.out.println("Current Capacity: "
                           + sb.capacity());

        // Ensure the capacity for 100 characters
        sb.ensureCapacity(100);
        System.out.println(
            "Capacity after ensuring space for 100 characters: "
            + sb.capacity());
    }
}

Output
Current Capacity: 16
Capacity after ensuring space for 100 characters: 100

Next Article
Article Tags :
Practice Tags :

Similar Reads