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 capacity() Method

Last Updated: October 7, 2022 by Chaitanya Singh | Filed Under: java

Java StringBuilder capacity() method returns the current capacity of StringBuilder object. In this tutorial, we will discuss the capacity() method in detail with the help of examples.

StringBuilder sb = new StringBuilder(); //default capacity 16
StringBuilder sb = new StringBuilder(34); //capacity 34

Syntax of capacity() method:

int cp = sb.capacity() //returns the capacity of sb

Here, sb is an object of StringBuilder class.

capacity() Description

public int capacity(): Returns the current capacity. If the current capacity is exhausted by adding new elements, the capacity is automatically increased using the following formula:

New Capacity = (Old Capacity*2)+2

capacity() Parameters

  • It does not take any parameter.

capacity() Return Value

  • It returns an integer value, which represents the current capacity of StringBuilder object.

Example 1: Capacity for various sequences

class JavaExample{
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("TEST STRING");
    /* Capacity of newly created StringBuilder instance
     * = length of string "TEST STRING" + 16
     * i.e 11+16 =27
     */
    System.out.println("Case1: Capacity: "+sb.capacity());

    StringBuilder sb2 = new StringBuilder(); //default 16
    System.out.println("Case2: Capacity: "+sb2.capacity());

    StringBuilder sb3 = new StringBuilder("ABC");
    /* Capacity = length of String "ABC"+ default capacity
     * i.e 3+16 =19
     */
    System.out.println("Case3: Capacity: "+sb3.capacity());

  }
}

Output:

Java StringBuilder capacity() Example Output_1

Example 2: Capacity Increase when limit exhausted

class JavaExample{
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    System.out.println("Capacity: "+sb.capacity()); //default 16
    sb.append("Text"); // capacity is not exhausted
    System.out.println("Capacity: "+sb.capacity());
    //appending 15 chars in the sequence
    //so sequence now has 19 char, which cant be adjusted
    // in default capacity so capacity is increased
    // new capacity = (16*2)+2 = 34
    sb.append("123456789101112");
    System.out.println("Capacity: "+sb.capacity());
  }
}

Output:

Java StringBuilder capacity() Example Output_2

Example 3: Free extra capacity

To free up the capacity, we have used trimToSize() method.

class JavaExample{
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    sb.append("Welcome to BeginnersBook.com"); //16*2+2 = 34
    System.out.println("Capacity: "+sb.capacity()); //34

    //we appended 28 characters, capacity is increased from 16 to 34
    //if we want to trim extra capacity and free memory
    sb.trimToSize();
    System.out.println("Capacity after trim: "+sb.capacity());
  }
}

Output:

Java StringBuilder capacity() Example Output_3

Recommended Posts

  • Java StringBuilder ensureCapacity()
  • Java StringBuilder length()
  • Java StringBuilder append()
  • Java StringBuilder subSequence()

Top Related Articles:

  1. how to copy one hashmap content to another hashmap
  2. Java StringBuffer toString()
  3. Java StringBuilder delete()
  4. Java StringBuilder offsetByCodePoints()
  5. Java StringBuffer class With Examples

Tags: Java-StringBuilder

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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 – 2025 BeginnersBook . Privacy Policy . Sitemap