The Wayback Machine - https://web.archive.org/web/20240726021509/https://www.geeksforgeeks.org/stringbuffer-ensurecapacity-method-in-java-with-examples/
Open In App

StringBuffer ensureCapacity() method in Java with Examples

Last Updated : 28 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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

  • If the current capacity of StringBuffer < the argument minimumCapacity, then a new internal array is allocated with greater capacity.
  • If the minimumCapacity argument > twice the old capacity, plus 2 then new capacity is equal to minimumCapacity else new capacity is equal to twice the old capacity, plus 2.
  • If the minimumCapacity argument passed as parameter < 0, this method takes no action.

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)



Similar Reads

ArrayList ensureCapacity() method in Java with Examples
The ensureCapacity() method of java.util.ArrayList class increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter
2 min read
StringBuilder ensureCapacity() in Java with Examples
The ensureCapacity(int minimumCapacity) method of StringBuilder class helps us to ensures the capacity is at least equal to the specified minimumCapacity passed as the parameter to the method. If the current capacity of StringBuilder is less than the argument minimumCapacity, then a new internal array is allocated with greater capacity. If the mini
2 min read
Stack ensureCapacity() method in Java with Example
The ensureCapacity() method of Java.util.Stack class increases the capacity of this Stack instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter. Below
2 min read
Vector ensureCapacity() method in Java with Example
The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter. Belo
2 min read
StringBuffer deleteCharAt() Method in Java with Examples
The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of integer type which refers to the index of the char
2 min read
StringBuffer appendCodePoint() Method in Java with Examples
appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an integer value been assigned. --&gt; appendCodePoint(
3 min read
StringBuffer delete() Method in Java with Examples
The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_point, int end_point) Parameters : The method acce
3 min read
StringBuffer replace() Method in Java with Examples
The StringBuffer.replace() is the inbuilt method which is used to replace the characters in a substring of this sequence with the characters in the specified String. Here simply the characters in the substring are removed and other char is inserted at the start.Syntax : public StringBuffer replace(int first, int last, String st) Parameters : The me
2 min read
StringBuffer reverse() Method in Java with Examples
The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the StringBuffer after reversing the characters. Examples
2 min read
StringBuffer append() Method in Java with Examples
Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boolean a) is an inbuilt method in Java which is used t
13 min read
Practice Tags :
three90RightbarBannerImg