StringBuilder capacity() Method In Java

StringBuilder capacity() Method In Java | In this blog we will discuss the capacity() method of the StringBuilder class which returns the capacity of the given string. The capacity is the number of toal characters StringBuilder object can contain.

The syntax of the capacity method is as follows:- public int capacity()
Parameters: It doesn’t accept any parameters.
Return type: integer.
Returns: The capacity of the string.

Default Capacity of StringBuilder In Java

For any empty string, the default capacity is 16 characters. Once we call the append() method to add the characters and if the number of characters increases above 16 then the capacity can be calculated by the given formula = (old-capacity * 2) + 2

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder();
      System.out.println("Default capacity = " + sb.capacity());
   }
}

Output:-

Default capacity = 16

Initial Capacity Of StringBuilder Java

The default capacity of StringBuilder is 16 where StringBuilder is empty. But if we initialize the StringBuilder object with some string then the initial capacity = default capacity + length of the string.

public class Main {
   public static void main(String[] args) {
      String string = "Java";
      System.out.println("String length: " + string.length());
      
      StringBuilder sb = new StringBuilder(string);
      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());
   }
}

Output:-

String length: 4
StringBuilder: Java
Capacity: 20

The default capacity = 16, length of the string = 4. Hence the initial capacity = 16 + 4 = 20

public class Main {
   public static void main(String[] args) {
      String string = "Java Program";
      System.out.println("String length: " + string.length());
      
      StringBuilder sb = new StringBuilder(string);
      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());
   }
}

Output:-

String length: 12
StringBuilder: Java Program
Capacity: 28

The default capacity = 16, length of the string = 12. Hence the initial capacity = 16 + 12 = 28

public class Main {
   public static void main(String[] args) {
      System.out.println("Length: " + "Java Programming Language".length());
      StringBuilder sb = new StringBuilder("Java Programming Language");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());
   }
}

Output:-

Length: 25
StringBuilder: Java Programming Language
Capacity: 41

The default capacity = 16, length of the string = 25. Hence the initial capacity = 16 + 25 = 41.

StringBuilder capacity() Java Example

Let us see some examples of the StringBuilder capacity() method. The initial capacity will be 16 characters, and after inserting the 16 characters the capacity will be increased to 34. Similarly, once the number of characters reached 34 then the capacity will be increased to 70. Let us see it through an example. New capacity = (old-capacity * 2) + 2

public class Main {
   public static void main(String[] args) {

      StringBuilder sb = new StringBuilder();
      System.out.println("StringBuilder: " + sb);
      System.out.println("Length: " + sb.length());
      System.out.println("Default capacity: " + sb.capacity());

      sb.append("Java Programming");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Length: " + sb.length());
      System.out.println("Current capacity: " + sb.capacity());

      sb.append("-");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Length: " + sb.length());
      System.out.println("Current capacity: " + sb.capacity());
   }
}

Output:-

StringBuilder:
Length: 0
Default capacity: 16
StringBuilder: Java Programming
Length: 16
Current capacity: 16
StringBuilder: Java Programming-
Length: 17
Current capacity: 34

When the number of characters was 16 till then the capacity was 16. But once we add one more character and the number of characters exceeds 16 then the capacity becomes 34.

Java StringBuilder capacity()

From the above examples, don’t conclude that capacity will be always 16, 34, 70, and so on. The formula is old-capacity * 2 + 2. If the initial capacity is 20, then the new capacity will be 20 * 2 + 2 = 42. The below example demonstrates it better.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder();
      System.out.println("Default capacity: " + sb.capacity());

      StringBuilder sb1 = new StringBuilder("Hello");
      System.out.println("Initial Capacity: " + sb1.capacity());

      sb1.append(", World! ");
      System.out.println("New capacity: " + sb1.capacity());

      sb1.append("Java Programing Language");
      System.out.println("New capacity: " + sb1.capacity());

      sb1.append("-KnowProgram");
      System.out.println("New capacity: " + sb1.capacity());
   }
}

Output:-

Default capacity: 16
Initial Capacity: 21
New capacity: 21
New capacity: 44
New capacity: 90

Java StringBuilder capacity() Method Example

From the above example, we may think that capacity of a given StringBuilder object will always increase but this is not true. If we change the StringBuilder object then the capacity of the StringBuilder object can also be decreased.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Learn Java Programming Language");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());

      sb = new StringBuilder("Hi");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());
   }
}

Output:-

StringBuilder: Learn Java Programming Language
Capacity: 47
StringBuilder: Hi
Capacity: 18

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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