The Java length() method
This is one of the articles from our Java Tutorial for Beginners.
The Java String length() Method
The length() method is used to find a String's length (the number of characters in a String).
The signature of the Java String length() method is below:
1 |
public int length() |
Example:
1 2 3 4 5 6 7 |
class Test { public static void main(String[] args) { String sentence = "I adore Java."; System.out.println(sentence.length()); } } |
If you run this code on your computer, you will see the following in the console:
13
Commentary:
1 |
System.out.println(sentence.length()); |
This line of code allows to display the length in characters of the sentence "I adore Java." We did this with the help of the length() method. If you count all the letters and spaces and the period in the sentence "I adore Java.", you will get the number 13.
As you see, everything is very simple.
You can find more articles in our Java Tutorial for Beginners.