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

By Chaitanya Singh | Filed Under: java

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

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

Syntax of capacity() method

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

Here, sb is an object of StringBuffer 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 StringBuffer object.

Example 1: Capacity of different char sequences

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Nice Guide");
    /* Capacity of newly created StringBuffer instance
     * = length of string "Nice Guide" + 16
     * i.e 10+16 =26
     */
    System.out.println("Case 1: Capacity: "+sb.capacity());

    StringBuffer sb2 = new StringBuffer(); //default 16
    System.out.println("Case 2: Capacity: "+sb2.capacity());

    StringBuffer sb3 = new StringBuffer("Hello");
    /* Capacity = length of String "Hello"+ default capacity
     * i.e 5+16 =21
     */
    System.out.println("Case3: Capacity: "+sb3.capacity());
  }
}

Output:

Java StringBuffer capacity() Output 1

Example 2: Capacity automatically increased

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    System.out.println("Capacity: "+sb.capacity()); //default 16
    sb.append("Welcome"); // capacity is not exhausted
    System.out.println("Capacity: "+sb.capacity());

    //appending 17 new chars in the sequence
    //so sequence now has 24 chars, which cant be adjusted
    // in default capacity so capacity is increased
    // new capacity = (16*2)+2 = 34
    sb.append(" to beginnersbook");
    System.out.println("Capacity: "+sb.capacity());
  }
}

Output:

Java StringBuffer capacity() Output 2

Example 3: Trim non utilized capacity

To trim the free capacity, we can use StringBuffer trimToSize() method as shown below:

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    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 StringBuffer capacity() Output 3

Recommended Posts

  • Java StringBuffer toString()
  • Java StringBuffer getChars()
  • Java StringBuffer indexOf()
  • Java StringBuffer lastIndexOf()
❮ 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