Java String length() Method

In this tutorial, we will learn about Java String length() Method.

Most of the times, we need to calculate the length of a String. String class in Java provides a method length(). The String length() method in Java is used to calculate the length of a string. The length is equal to the number of characters in the string.

Syntax:

The syntax of length() method is:

length() method returns the length of the string. The length is equal to the number of characters in the string.

Example:

Let’s see length() method with an example.

Output:

In the above program, we have taken a String “This is a String and we are calculating the length of this String.” in str object and we have calculated length of String str using length() method.

You can also learn about String equals() method at Java String equals() Method.