Java StringBuffer length() Method

Java StringBuffer length() Method | The length() method of the StringBuffer class returns the number of characters in the StringBuffer object. We can directly obtain the length of the string without the StringBuffer class but it might create a new object hence it consumes more space and performance decreases.

An example of the length method is as follows:-
Example-1:-
StringBuffer: “People”
Length: 5
Example-2:
StringBuffer: “”
Length: 0

StringBuffer length() Method in Java

The syntax for the length method is as follows:- public int length()
The length() method doesn’t take any parameters
Returns: the length of the StringBuffer object containing a string or sequence of characters.
Return type: int

How To Use StringBuffer Length In Java

In the below program we have used three strings of different lengths, first, we have used the string of medium characters and returned the length of the same using the length method and then we have used the empty string and returned the length of the same, later on, we have used the string which is quite lengthy which has 58 characters.

public class Main {
    public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer("Garden");
        System.out.println("StringBuffer = " + sb1);
        System.out.println(sb1.length());
        
        StringBuffer sb2 = new StringBuffer("");
        System.out.println("StringBuffer = " + sb2);
        System.out.println(sb2.length());
        
        String str = "Business is a money game with few rules and a lot of risk.";
        StringBuffer sb3 = new StringBuffer(str);
        System.out.println("StringBuffer = " + sb3);
        System.out.println(sb3.length());
    }
}

Output:-

StringBuffer = Garden
6
StringBuffer =
0
StringBuffer = Business is a money game with few rules and a lot of risk.
58

StringBuffer Length() Method In Java Example

In this program, we are taking the user input and printing the length of the same. If you observe the program, you will come to know that we have used the same StringBuffer object for two strings hence the output that is the length will be a total of two strings.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        StringBuffer string = new StringBuffer("");
        System.out.print("Enter some string: ");
        Scanner scan = new Scanner(System.in);
        string.append(scan.nextLine());
        System.out.println("The string is: " + string);
        System.out.println("Length of your string: " + string.length());

        System.out.print("\nPlease enter some string again: ");
        string.append(scan.nextLine());
        System.out.println("The string is: " + string);
        System.out.println("Length of your string:" + string.length());
        scan.close();
    }
}

Output:-

Enter some string: Hello
The string is: Hello
Length of your string: 6

Please enter some string again: How are you?
The string is: Hello How are you?
Length of your string:18

What is the Default Length of A Java StringBuffer

Initially, the StringBuffer object doesn’t contain any character therefore the default length of the StringBuffer object is 0.

public class Main {
    public static void main(String[] args) {
        StringBuffer string = new StringBuffer();
        System.out.println("Length of your string: " + string.length());
    }
}

Output:-

Length of your string: 0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *