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 StringBuffer ensureCapacity()

By Chaitanya Singh | Filed Under: java

Java StringBuffer ensureCapacity() method ensures that specified minimum capacity is maintained. If the current capacity is less than the specified minimum capacity then the capacity is increased.

Syntax of ensureCapacity() method

sb.ensureCapacity(25) //if current capacity < 25 then increase capacity

Here, sb is an object of StringBuffer class.

ensureCapacity() Description

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

New Capacity  = (Old Capacity*2) +2

ensureCapacity() Parameters

It takes a single parameter:

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

ensureCapacity() Return Value

  • It does not return anything.

Example

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

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

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

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

Output:

Java StringBuffer ensureCapacity() Output

Recommended Posts

  • Java StringBuffer capacity()
  • Java StringBuffer toString()
  • Java StringBuffer getChars()
  • Java StringBuffer indexOf()
❮ Java StringBuffer

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