length() - String Methods in Java
This article is a part of
String Methods in Java.
The length() method returns the length of the specified string.
Return type
The return type for length() method is int. The length of an empty string is 0. Let us look at an example code.
Output
Let us look at the output of the above.
Note: The charAt() method starts its counter from 0 whereas
length() starts its counter from
1. However, the length of an empty string is 0.
While we are at it, let us look at another method related to length().
isEmpty()
The isEmpty() method checks if the string is empty or not. It works on the method length() by checking if the length() of the string is 0 or not.
Return type
The return type of isEmpty() method is Boolean. It returns true if the
string is empty and
false otherwise.
Let us look at an example code.
Output
Let us look at the output of the above.