StringBuilder capacity() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 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. Create Quiz Comment A AmanSingh2210 Follow 0 Improve A AmanSingh2210 Follow 0 Improve Article Tags : Java java-basics Java-Functions Java-StringBuilder Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like