BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuilder ensureCapacity()

By Chaitanya Singh | Filed Under: java

In this tutorial, we will discuss the Java StringBuilder ensureCapacity() method with the help of examples. This method ensures that a minimum capacity is maintained. If the current capacity is less than the specified minimum capacity then the capacity is increased.

The syntax of ensureCapacity() method is:

sb.ensureCapacity(34) //if capacity is less than 34 then increase capacity

Here, sb is an object of StringBuilder class.

ensureCapacity() Description

public void ensureCapacity(int min): If the current capacity of StringBuilder instance is less than the specified minimum capacity min, then the capacity of StringBuilder instance is increased using the following formula:

New Capacity  = (Old Capacity*2) +2

ensureCapacity() Parameters

The ensureCapacity() method of Java StringBuilder class takes a single parameter:

  • min: This is an integer parameter that represents the minimum capacity that needs to be maintained.

ensureCapacity() Return Value

This method has void return type, which means it does not return anything.

Example of ensureCapacity() method

public class JavaExample {
  public static void main(String[] args) {
    //created a StringBuilder instance using default constructor
    StringBuilder sb = new StringBuilder();

    //default capacity is 16
    System.out.println("Initial Capacity: " + sb.capacity());

    //If we are expecting an addition to the StringBuilder instance
    //which is greater than the default 16 then we can increase the
    //capacity before the operation using ensureCapacity
    sb.ensureCapacity(25); // 16*2 + 2 = 34
    System.out.println("New Capacity: " + sb.capacity());
    sb.append("Welcome to BeginnersBook");

    System.out.println("String: " + sb);
    System.out.println("Current Capacity: " + sb.capacity());
  }
}  

Output:

Java StringBuilder ensureCapacity Example Output

Recommended Posts

  • Java StringBuilder substring()
  • Java StringBuilder charAt()
  • Java StringBuilder capacity()
  • Java StringBuilder append()

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap