Java – String Length
To get the length of String in Java, you can use length() method. length() method returns an integer representing the number of characters in the string.
In Java, you can store a string using a String class, or you may be building or transforming a much bigger string using a StringBuilder. In this tutorial, we will learn how to find the length of a string in Java, during different scenarios.
Example 1 – String Length using String.length()
In this example, we shall initialize a string using String class. After that, we shall call length() method on the string and print the length to the console output.
StringLength.java
/**
* Java Example Program to find String Length
*/
public class StringLength {
public static void main(String[] args) {
//initialize a string
String str = "tutorialkart";
//get string length
int len = str.length();
System.out.print(len);
}
}
Run the program from command prompt or run it from your IDE, like Eclipse, etc.
Output
12
The number of characters in the string we have taken are 12, hence the length is 12.
Example 2 – String Length using StringBuilder.length()
In this example, we shall initialize a StringBuilder, do some operations with the StringBuilder, and then find the length of the string in StringBuilder using length() method.
StringLength.java
/**
* Java Example Program to find String Length
*/
public class StringLength {
public static void main(String[] args) {
//you are building a string using string builder
StringBuilder sb = new StringBuilder();
//some operations on string builder
sb.append("tutorialkart");
sb.append(".com");
//get string length
int len = sb.length();
System.out.print(len);
}
}
Run the program. As the total string is "tutorialkart.com"
, where the number of characters is 17, we get 17 as the output.
Output
16
Example 3 – Length of Empty String
In this example, we shall take an empty string and try to find its length. Of course, we have to get zero as length, but let us try this scenario for example.
StringLength.java
/**
* Java Example Program to find String Length
*/
public class StringLength {
public static void main(String[] args) {
//empty string
String str = "";
int len = str.length();
System.out.println(len);
}
}
Run the program. As the total string is "tutorialkart.com"
, where the number of characters is 17, we get 17 as the output.
Output
0
Example 4 – Length of Null String
In this example, we shall take an string and initialize with null. We shall try to find its length by calling length() method on the string. In that case, String.length() throws NullPointerException.
StringLength.java
/**
* Java Example Program to find String Length
*/
public class StringLength {
public static void main(String[] args) {
//null object in string
String str = null;
int len = str.length();
System.out.println(len);
}
}
Run the program and you shall see following exception thrown at runtime.
Output
Exception in thread "main" java.lang.NullPointerException
at StringLength3.main(StringLength3.java:10)
Conclusion
In this Java Tutorial, we learned how to find the string length in different Scenarios like using String class, StringBuffer class, and when the string is empty and null.