A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/stringbuffer-ensurecapacity-method-in-java-with-examples/ below:

StringBuffer ensureCapacity() method in Java with Examples

StringBuffer ensureCapacity() method in Java with Examples

Last Updated : 28 Dec, 2022

The ensureCapacity() method of StringBuffer class ensures the capacity to at least equal to the specified minimumCapacity.

Syntax:

public void ensureCapacity(int minimumCapacity)

Parameters: This method takes one parameter minimumCapacity which is the minimum desired capacity. Return Value: This method do not return anything. Below programs demonstrate the ensureCapacity() method of StringBuffer Class Example 1: 

Java
// Java program to demonstrate
// the ensureCapacity() Method.

class GFG {
    public static void main(String[] args)
    {

        // create a StringBuffer object
        StringBuffer str = new StringBuffer();

        // print string capacity
        System.out.println("Before ensureCapacity "
                           + "method capacity = "
                           + str.capacity());

        // apply ensureCapacity()
        str.ensureCapacity(18);

        // print string capacity
        System.out.println("After ensureCapacity"
                           + " method capacity = "
                           + str.capacity());
    }
}
Output:
Before ensureCapacity method capacity = 16
After ensureCapacity method capacity = 34

Example 2: 

Java
// Java program to demonstrate
// the ensureCapacity() Method.

class GFG {
    public static void main(String[] args)
    {

        // create a StringBuffer object
        StringBuffer
            str
            = new StringBuffer("Geeks For Geeks");

        // print string capacity
        System.out.println("Before ensureCapacity "
                           + "method capacity = "
                           + str.capacity());

        // apply ensureCapacity()
        str.ensureCapacity(42);

        // print string capacity
        System.out.println("After ensureCapacity"
                           + " method capacity = "
                           + str.capacity());
    }
}
Output:
Before ensureCapacity method capacity = 31
After ensureCapacity method capacity = 64

References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#ensureCapacity(int)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4