Java StringBuffer length() Method Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The length() method in the StringBuffer class is used to retrieve the number of characters currently in a StringBuffer object. It helps determine the current size of the character sequence in the buffer.Example 1: Here, we are using the length() method to retrieve the number of characters in a StringBuffer object. Java // Java program to demonstrate length() method public class StringBufferLength { public static void main(String[] args) { // Create a StringBuffer object with a string StringBuffer sb = new StringBuffer("Hello, GFG"); System.out.println("" + sb.length()); } } Output10 Syntax of length() Methodpublic int length()Parameter: None.Return Type: int (returns the length of the StringBuffer object).Example 2: In this example, we will use the length() method to reflects all the changes after modifying the StringBuffer. Java // Java program to demonstrate length() // after modifications public class StringBufferLength { public static void main(String[] args) { // Create a StringBuffer object StringBuffer sb = new StringBuffer("Java Programming"); // Print the initial length of the StringBuffer System.out.println("" + sb.length()); // Append a string to the StringBuffer sb.append(" Language"); System.out.println("" + sb.length()); } } Output16 25 Example 3: This example demonstrates how length() behaves for an empty StringBuffer object. Java // Java program to demonstrate length() with // an empty StringBuffer public class StringBufferLength { public static void main(String[] args) { // Create an empty StringBuffer object StringBuffer sb = new StringBuffer(); // Print the length of the empty StringBuffer System.out.println("" + sb.length()); } } Output0 Example 4: Here, we will use the length() method to perform conditional checks on the content of a StringBuffer. Java // Java program to use length() // for conditional checks public class StringBufferLength { public static void main(String[] args) { // Create a StringBuffer object with a string StringBuffer sb = new StringBuffer("Java Programming"); // Check if the StringBuffer is empty if (sb.length() == 0) { System.out.println("StringBuffer is empty."); } else { System.out.println("StringBuffer is not empty. Length: " + sb.length()); } } } OutputStringBuffer is not empty. Length: 16 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 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 LongBuffer slice() method in Java The slice() method of java.nio.LongBuffer Class is used to creates a new Long buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, an 3 min read ShortBuffer limit() method in Java with Examples The limit() method of java.nio.ShortBuffer Class is used to modify this ShortBuffer's limit. This method takes the limit to be set as the parameter and sets that as the new limit of this Buffer. If the mark of this Buffer is already defined and is larger than the new specified limit, then this new l 3 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 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 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