Java StringBuffer capacity() Method Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 use of the capacity() method to check the initial capacity of a StringBuffer object. Java // Java program to demonstrate capacity() // method of StringBuffer public class Geeks { public static void main(String[] args) { // Create a StringBuffer object // with no initial content StringBuffer sb = new StringBuffer(); // Display the default capacity of StringBuffer System.out.println("Default Capacity: " + sb.capacity()); } } OutputDefault Capacity: 16 Note: By default, the initial capacity of a StringBuffer is 16 characters and it is same for the StringBuilder class as well.Syntax of capacity() Methodpublic int capacity()Return Type: The method returns an int that represents the current capacity of the StringBuilder or StringBuffer object.Example 2: The below Java program demonstrates how the capacity of a StringBuffer changes when text is added to exceed its current capacity. Java // Java program to demosntrates the // dynamic changes in capacity public class Geeks { public static void main(String[] args) { // Create a new StringBuffer StringBuffer sb = new StringBuffer(); // Checking the default capacity System.out.println("Default capacity: " + sb.capacity()); // Adding some text to the StringBuffer sb.append("Hellogfg"); System.out.println( "Capacity after adding some text: " + sb.capacity()); // Adding more text to exceed the default capacity sb.append( " Hello Coders Welcome to GeeksForGeeks."); System.out.println( "Capacity after adding more text: " + sb.capacity()); } } OutputDefault capacity: 16 Capacity after adding some text: 16 Capacity after adding more text: 47 Explanation: In the above example, initially, the default capacity is 16. Then we have added text within the initial capacity which does not increase the capacity. When the text exceeds the current capacity, the new capacity is generally calculated as (old capacity * 2) + 2. For example,When 16 is exceeded, the capacity is expected to become (16 * 2) + 2 = 34.Since the appended content requires more space, Java adjusts the capacity to 47 by ensuring enough space for the additional text.Example 3: The below Java program demonstrates a null reference cause a NullPointerException, if we try to call the capacity() method on it. Java // Java program to demonstrate a NullPointerException public class Geeks { public static void main(String[] args) { // Null reference StringBuffer sb = null; try { // trying to call capacity() on // a null reference System.out.println(sb.capacity()); } catch (NullPointerException e) { System.out.println( "Caught NullPointerException: " + e.getMessage()); } } } Output: Note: A Stringbuffer can be initialized as null, but attempting to call a method on it will cause a NullPointerException. Comment More infoAdvertise with us Next Article Java StringBuffer capacity() Method J juhisrivastav Follow Improve Article Tags : Java Java-Strings java-StringBuffer Practice Tags : JavaJava-Strings Similar Reads StringBuilder capacity() Method in Java 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/ 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 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 Class in Java The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c 11 min read StringBuffer trimToSize() method in Java with Examples The trimToSize() method of StringBuffer class is the inbuilt method used to trims the capacity used for the character sequence of StringBuffer object. If the buffer used by StringBuffer object is larger than necessary to hold its current sequence of characters, then this method is called to resize t 2 min read ShortBuffer compact() method in Java With Examples The compact() method of java.nio.ShortBuffer Class is used to compact the given buffer. The shorts between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the short at index p = position() is copied to index zero, the short at index p + 1 is c 3 min read LongBuffer compact() method in Java The compact() method of java.nio.LongBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set 3 min read ShortBuffer allocate() method in Java With Examples The allocate() method of java.nio.ShortBuffer Class is used to allocate a new short buffer.The position of the new Buffer will be zero & it's limit is its capacity, though the mark is undefined, and each of its elements is initialized to zero. It will be having a backing array, and the array off 2 min read Java ensureCapacity(int minimumCapacity) Method In Java, the ensureCapacity(int minimumCapacity) method of the StringBuilder and StringBuffer classes is used to ensure that the buffer has a minimum capacity. If the current capacity is less than the specified minimum, it will increase the capacity to accommodate the required size.Example 1: The be 3 min read ShortBuffer array() Method in Java with Examples The array() method of java.nio.ShortBuffer is used to return the short array that backs this buffer(optional). Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke the hasArray method before invoking this method in order to ensure that 3 min read Like