Java StringBuffer length() Method



The Java StringBuffer length() method is used to get the length of the StringBuffer object. A length is simply the number of characters in the String/StringBuffer. If the current StringBuffer object is empty, this method returns zero.

The length() method does not accept any parameter. It does not throw any exception while retrieving the length of the string.

Note − If the string is an empty string but contains some white-spaces, then the length() method counts the white-spaces and returns it as the length of the string.

Syntax

Following is the syntax of the Java StringBuffer length() method −

public int length()

Parameters

  • It does not accept any parameter.

Return Value

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

Example

If the value of the StingBuilder object is not null, this method returns the length of the string.

In the following example, we are creating an object of the StringBuffer with the value “TutorialsPoint”. Using the length() method, we are retrieving the length of the current StringBuffer object.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("TutorialsPoint");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: TutorialsPoint
The length of the 'TutorialsPoint' is: 14

Example

If the current StringBuffer is an empty string, the length() method returns 0.

In the following program, we are instantiating the StringBuffer with an empty value. Using the length() method, we are trying to retrieve the length of this StringBuffer object.

import java.lang.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

Following is the output of the above program −

The given string is: 
The length of the '' is: 0

Example

In the following example, we are creating an object of the StringBuffer with the value “HelloWorld”. Then, we are using the for loop to iterate through the length() of the string. Using the charAt() method, we are trying to print each character of the current object.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("HelloWorld");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
      //using for loop
      System.out.print("The string characters are: ");
      for(int i = 0; i<length; i++) {
         //using the charAt() method
         System.out.print(sb.charAt(i) + " ");
      }
   }
}

Output

The above program, produces the following results −

The given string is: HelloWorld
The length of the 'HelloWorld' is: 10
The string characters are: H e l l o W o r l d

Example

If the current object just contains some white-spaces, the length() method counts the white space and returns it as string length.

In the following program, we are instantiating the StringBuffer with an empty value, but it contains 4 white-spaces. Using the length() method, we are trying to retrieve its length.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      //instantiating  StringBuffer
      StringBuffer sb = new StringBuffer("   ");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

After executing the above program, it will produce the following output −

The given string is:
The length of the '   ' is: 3
java_lang_stringbuffer.htm
Advertisements