Java StringBuilder length() Method

Java StringBuilder Length | In some situations, there need to find the length of the given sentence, in such a situation we can’t sit and count each and every letter in the sentence. Hence to overcome this issue Java provides the method to count the number of letters in the sentence or the string. In the StringBuilder class, we have a length method that returns the number of characters in the StringBuilder object.

The syntax for the length() method is as follows: public int length()
Parameters: it will not take any parameters
Return type: int
Returns: the length of the string.

Example-1:
String: “Doll”
length: 3

Example-2:
String: “Paper”
Length: 4

Java StringBuilder length() Method

We have passed the sentence i.e. a string to the StringBuilder constructor. Then called the length method and store the result in the variable called len and print the len to see the result.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Papaya Garden");
      System.out.println("StringBuilder = " + string);
      int len = string.length();
      System.out.println("Length = " + len);
   }
}

Output:

StringBuilder = Papaya Garden
Length = 13

StringBuilder Length Java Example

Let us see another example of the StringBuilder length() Method in Java. We will follow the same thing as above that is we are passing a sentence to the string builder class and then converting it to a string using the toString() method and storing the length in the len variable and then printing the same.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("God is Great");
      System.out.println("String = " + string.toString());
      int len = string.length();
      System.out.println("Length of String = " + len);
   }
}

Output:

String = God is Great
Length of String = 12

Java StringBuilder Length Example

If the given StringBuilder object is empty then the length() method will return 0 else StringBuilder object doesn’t contain an object. Below program demonstrates it:-

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

Output:-

Length = 9
Length = 0

If the given StringBuilder object is null then on calling the length() method we will get NullPointerException.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = null;
      System.out.println("Length = " + string.length());
   }
}

Output:-

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “java.lang.StringBuilder.length()” because “string” is null
at Main.main(Main.java:4)

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 *