Get the length of a StringBuffer Object in Java

Java 8Object Oriented ProgrammingProgramming

To find the length of the StringBuffer object, the length() function is used. It is a method of the StringBuffer Class. The method returns the count of characters in the sequence.

Declaration- The java.lang.StringBuffer.length() method is declared as follows −

public int length()

Let us see an example program illustrating the use of length() method −

Example

 Live Demo

public class Example {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Hello World");
      int len = sb.length(); // computes the length of the StringBuffer Object
      System.out.println(len);
   }
}

Output

11
raja
Published on 03-Jan-2019 15:50:55
Advertisements