StringBuilder capacity() Method in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the capacity() method of StringBuilder Class is used to return the current capacity of StringBuilder object. The capacity is the amount of storage available to insert new characters.Example 1: Here, we use the capacity() method to check the current capacity of the StringBuilder object. Java // Java program to demonstrate // the capacity() Method in StringBuilder class Geeks { public static void main(String[] args) { // create a StringBuilder object, // default capacity will be 16 StringBuilder sb = new StringBuilder(); // get default capacity int c = sb.capacity(); System.out.println("Default Capacity of StringBuilder: " + c); // add the String to StringBuilder Object sb.append("Geek"); // get capacity c = sb.capacity(); System.out.println("StringBuilder: " + sb); System.out.println("Current Capacity of StringBuilder: " + c); } } OutputDefault Capacity of StringBuilder: 16 StringBuilder: Geek Current Capacity of StringBuilder: 16 Syntax of StringBuilder capacity() Method public int capacity()Return Value: This method returns the current capacity of StringBuilder class.Example 2: Here, we use the capacity() method with a String Passed as Parameter. Java // Using capacity() Method with a String Passed as Parameter class Geeks { public static void main(String[] args) { // create a StringBuilder object // with a String passed as parameter StringBuilder sb = new StringBuilder("WelcomeGeeks"); // get capacity int c = sb.capacity(); System.out.println("StringBuilder: " + sb); System.out.println("Capacity of StringBuilder: " + c); } } OutputStringBuilder: WelcomeGeeks Capacity of StringBuilder: 28 Example 3: Here, we use the capacity() method on a null StringBuilder object. This will throw a NullPointerException. Java // Handling NullPointerException with // StringBuilder capacity() import java.io.*; class Geeks { public static void main(String[] args) { // Create a null StringBuilder object StringBuilder sb = null; try { int c = sb.capacity(); System.out.println("Capacity: " + c); } catch (NullPointerException e) { // Handle the exception System.out.println( "Error: Cannot call capacity() on a null StringBuilder."); } } } OutputError: Cannot call capacity() on a null StringBuilder. Comment More infoAdvertise with us Next Article Java StringBuffer capacity() Method A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Functions Java-StringBuilder Practice Tags : Java Similar Reads Java StringBuffer capacity() Method In Java, the capacity() method is used in the StringBuilder and StringBuffer classes to retrieve the current capacity of the object. The capacity is the amount of space that has been allocated to store the string that can grow dynamically as needed.Example 1: The below Java program demonstrates the 3 min read StringBuilder append() Method in Java In Java, the append() method of StringBuilder class is used to add data to the end of an existing StringBuilder object. It supports appending different data types like strings, integers, characters, and booleans by making it easy for dynamic string manipulation.Example 1: Here, we use the append() m 4 min read Vector capacity() Method in Java The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.Syntax of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 min read StringBuffer ensureCapacity() method in Java with Examples The ensureCapacity() method of StringBuffer class ensures the capacity to at least equal to the specified minimumCapacity. If the current capacity of StringBuffer < the argument minimumCapacity, then a new internal array is allocated with greater capacity.If the minimumCapacity argument > twic 2 min read StringBuilder appendCodePoint() method in Java with Examples The appendCodePoint(int codePoint) method of StringBuilder class is the inbuilt method used to append the string representation of the codePoint argument to this sequence. The argument is appended to this StringBuilder content and length of the object is increased by Character.charCount(codePoint). 2 min read StringBuilder length() in Java with Examples The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o 2 min read Like