Home » Java programming language

Java StringBuilder ensureCapacity() method with example

StringBuilder Class ensureCapacity() method: Here, we are going to learn about the ensureCapacity() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019

StringBuilder Class ensureCapacity() method

  • ensureCapacity() method is available in java.lang package.
  • ensureCapacity() method is used to check the ensurity of the capacity with the given capacity (i.e. we ensure that the capacity is equal to the given argument).
    In this method let suppose argument capacity > current capacity so in that case new space allocated for an array with larger capacity so the new capacity will be:
        New Capacity = New Capacity > min_cap
        New Capacity = 2*old Capacity +2
    Note: When we pass the negative value as an argument so in that case no action will perform and simply returns
  • ensureCapacity() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • ensureCapacity() method does not throw an exception at the time of the ensurity of the capacity.

Syntax:

    public void ensureCapacity(int min_cap);

Parameter(s):

  • int min_cap – represents the minimum capacity required to perform action.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void ensureCapacity(int min_cap)
// method of StringBuilder 


public class EnsureCapacity {
    public static void main(String[] args) {
        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World");

        // Display st_b
        System.out.println("st_b = " + st_b);

        // Display Current Capacity st_b i.e. 16+10
        System.out.println("st_b.capacity() = " + st_b.capacity());

        // By using ensureCapacity() method is to extend the capacity 
        // with the given amount to st_b object if required
        // it returns twice the old capacity + 2 i.e.(2*st_b+2)
        st_b.ensureCapacity(54);

        // Display Current Capacity st_b i.e.54
        System.out.println("st_b.ensureCapacity(54) = " + st_b.capacity());
    }
}

Output

st_b = Java World
st_b.capacity() = 26
st_b.ensureCapacity(54) = 54






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.