Java.lang.StringBuilder.length() Method


Advertisements


Description

The java.lang.StringBuilder.length() method returns the length (character count).

Declaration

Following is the declaration for java.lang.StringBuilder.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.StringBuilder.length() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

   public static void main(String[] args) {
  
   StringBuilder str = new StringBuilder("admin");
   // prints the length of StringBuilder
   System.out.println("length = " + str.length());
  
   str = new StringBuilder("");
   // prints the length of empty StringBuider
   System.out.println("length = " + str.length());
   }
}  

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

length = 5
length = 0

java_lang_stringbuilder.htm

Advertisements