In this section, we will learn what the String length() method is and how to use it in Java.
What is Java String length() Method?
The Java String length() method is used to get the length (number of characters) of a string value.
Java length() Method Syntax:
public int length()
length() Method Parameters:
The method does not take an argument.
length() Method Return Value:
The return value of this method is of type integer and is equal to the number of characters that the target string value has.
length() Method Exceptions:
This method does not throw an exception.
Example: using String length() method
public class Simple { public static void main(String[] args) { String s1 = "Be kind to one another"; System.out.println(s1.length()); } }
Output:
22