Skip to Content

Java StringBuffer length Example

  1. /*
  2.    Java StringBuffer length Example
  3.    This example shows how to get length of a StringBuffer object.
  4. */
  5.  
  6. public class JavaStringBufferLengthExample {
  7.  
  8.   public static void main(String[] args) {
  9.    
  10.     /*
  11.      int length() method of Java StringBuffer class returns the length
  12.      of a StringBuffer object.
  13.     */
  14.     StringBuffer sb = new StringBuffer("Hello World");
  15.     int len = sb.length();
  16.     System.out.println("Length of a StringBuffer object is : " + len);
  17.   }
  18. }
  19.  
  20. /*
  21. Output would be
  22. Length of a StringBuffer object is : 11
  23. */