Java StringBuilder capacity() Method



The Java StringBuilder capacity() method is used to return the current capacity of the StringBuilder. The capacity is the amount of storage available for newly inserted characters.

The capacity() method does not accept any parameters, and it is not throwing any exception while returning the capacity of the StringBuilder.

Note − An empty StringBuilder class contains the 16 bytes default capacity. The capacity of the string and character is different. Capacity of String “A” is = 1; Capacity of Character ‘A’ is = 65;

Syntax

Following is the syntax of the Java StringBuilder capacity() method −

public int capacity()

Parameters

  • It does not accept any parameters.

Return Value

This method returns the capacity of the current StringBuilder object.

Example

If the given sequence while instantiating the current StringBuilder object is not null, Java StringBuilder capacity() Method returns the capacity of the current StringBuilder object.

In the following program, we creating an object of the StringBuilder class with the value “Hello Welcome”. Using the capacity() method, we are trying to retrieve the capacity of this object.

package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Hello Welcome");
      System.out.println("String is: " + sb);
      
      //returns the current capacity of the StringBuilder i.e. 16 + 13 
      System.out.println("The capacity of the sequence is: " + sb.capacity());
   }
}

Output

On executing the above program, it will produce the following result −

String is: CompileOnline
The capacity of the sequence is: 29

Example

If the current StringBuilder object is empty, this method returns the default capacity of the StringBuilder, which is 16 bytes.

In this program, we are creating an object of the StringBuilder with an empty value. Using the capacity() method, we are trying to retrieve the capacity of it.

package com.tutorialspoint.StringBuilder;
public class Capacity {
   public static void main(String[] args) {
      
      //create an empty StringBuilder
      StringBuilder sb = new StringBuilder();
      System.out.println("The String: " + sb);
      
      //using capacity() method
      System.out.println("The capacity of an empty StringBuilder is: " + sb.capacity());
   }
}

Output

Following is the output of the above program −

The String: 
The capacity of an empty StringBuilder is: 16

Example

If the given sequence value is single character string, this method returns 17.

In the following example, we create a StringBuilder with the value “Z”. Using the capacity() method, we are trying to retrieve the capacity of the sequence. The capacity of the single string character is 1. So (16 + 1 = 17).

package com.tutorialspoint.StringBuilder;
public class Capacity {
   public static void main(String[] args) {
      
      //create an empty StringBuilder
      StringBuilder sb = new StringBuilder("Z");
      System.out.println("StringBuilder: " + sb);
      
      //using capacity() method
      System.out.println("The capacity of the StringBuilder is: " + sb.capacity());
   }
}

Output

The above program, produces the following results −

StringBuilder: Z
The capacity of the StringBuilder is: 17
java_lang_stringbuilder.htm
Advertisements