length() StringBuffer Example

length() StringBuffer Example


StringBuffer comes with many utility methods to do operations on strings apart String class own methods. One such method is length() StringBuffer.

StringBuffer class comes with two methods – length() to find the number of characters present in the buffer and capacity() method to know the buffer size.

Following is the length() StringBuffer method signature as defined in Java API.

  • public int length(): Returns the number of characters present in the buffer.
Following example uses length() StringBuffer method to count the characters existing in the StringBuffer object.


length() StringBuffer
Output screenshot on length() StringBuffer Example

1. Also refer capacity() to find the buffer capacity (or storing capability) of StringBuffer object.
2. There is similar method length() in String class to find the number of characters.

Leave a Reply

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