Java.lang.StringBuffer.length() Method

Advertisements


Description

The java.lang.StringBuffer.length() method returns the length (character count) of the sequence of characters currently represented by this object.

Declaration

Following is the declaration for java.lang.StringBuffer.length() method

public int length()

Parameters

  • NA

Return Value

This method returns the length of the sequence of characters currently represented by this object.

Exception

  • NA

Example

The following example shows the usage of java.lang.StringBuffer.length() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("Tutorials");
    // printing the length of stringbuffer
    System.out.println("length = " + buff.length());
  
    buff = new StringBuffer("");
    // printing the length of empty stringbuffer
    System.out.println("length = " + buff.length());
  }
}

Let us compile and run the above program, this will produce the following result:

length = 9
length = 0



Advertisements
Advertisements