Strings in Java are immutable objects that are created once and have no option for changing. The string is considered an object that represents the character sequence. The String class in Java creates and manipulates the specified strings. This class consists of various methods like length(), compareTo(), equals(), etc. to handle a variety of string operations.
In this article, the length() method of Java is elaborated to calculate the length of the string in Java.
How to Calculate/Find the Length of a String in Java?
To compute the length of the String in Java, the Java’s length() method is implemented. The string is declared in the object and the number of characters declared in the string is calculated using the length() method. This method consists of a variety of methods. For example, we can check if the string is empty, the total whitespaces in the declared string, and many others.
Below is the syntax of the length() method.
Nameofstring.length()
Below are the examples implemented to fetch the length of the strings according to different scenarios.
Example 1: Length of the String Using the length() Method
The most common and basic type of method to calculate the length is the length() method which returns the length of the specified string in the code below.
//Declare a class to find the length of the string
public class StringLength{
//The Main class of Java
public static void main(String args[]){
//Declare the string
String j1="Programming";
String j2="Ruby";
String j3="Javascript";
//Print the Length of the declared string using the length() method.
System.out.println("The length of Programming is: "+j1.length());
System.out.println("The length of Ruby is: "+j2.length());
System.out.println("The length of Javascript is: "+j3.length());
}}
In the code above:
- A class is declared as “StringLength”.
- In the Main class of Java, there are three strings declared as j1, j2, and j3 respectively.
- The length() method is used to print the lengths of the specified strings using the println() method.
Output
The below output describes the total number of characters available in the strings.

Example 2: Length of an Empty String
The example below depicts that when a string is empty a value of “0” is returned.
//Declare the class for a String
public class StringLength2 {
//The Main class of Java
public static void main(String[] args) {
//Declare a string
String s = "StringExample";
//The if statement with for loop to check the length
if(s.length()>0) {
System.out.println("The length of the String is: "+s.length());
}
//Here an empty string is declared to check the output
s = "";
if(s.length()==0) {
System.out.println("The String is empty : "+s.length());
}
}
}
In the above block of code:
- A class is declared as “StringExample”.
- The next step involves using the if statement along with the length() method to check the length of the declared string.
- An empty String is also declared to see what appears as the output.
- The println() method is responsible for printing the output.
Output
The output below depicts that the length of the entered string is “13” and “0” is the length of an empty string.

Example 3: The length() Method to Get the Number of Whitespaces
The code below depicts that the length() method is used to determine the number of whitespaces present between the strings.
//Declare the class in Java using a public constructor
public class StringLength3{
public static void main(String argvs[])
{
//Declare a string to check the length and whitespaces
String s = "Programming in Java ";
int Whitespace = s.length();
System.out.println("In the string: " + "'" + s + "'");
//Declare the length of no white spaces
s = s.replace(" ", "");
int NoWhitespace = s.length();
//Calculate the white spaces
int num = Whitespace-NoWhitespace ;
//Print the number of whitespaces present in the string
System.out.print("Total number of whitespaces present are: " + num);
}
}
In the above code block:
- A class is declared to check the whitespaces in the specified string.
- The length() method calculates the string with whitespaces and without whitespaces in the string.
- The total length() is stored in the integer-type variable “num”.
- The number of White spaces is printed using the println() method.
Output
The output below shows that there are three whitespaces in the given string i.e., after “Programming” between “in and Java” and at the end after “Java”.

Conclusion
The length of the specified string is calculated using the length() method of Java. This method in Java returns the total number of characters currently available in the specified String. In this write-up, we have depicted in detail the methods to calculate the total length of a String in Java.