English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java String length() method returns the length of the string.
The syntax of the length() method is:
string.length()
The length() method does not take any parameters.
The length() method returns the length of the given string.
The length is equal to the number of char values (code units) in the string.
class Main { public static void main(String[] args) { String str1 = "Java"; String str2 = ""; System.out.println(str1.length()); // 4 System.out.println(str2.length()); // 0 System.out.println("Java".length()); // 4 System.out.println("Java\n".length()); // 5 System.out.println("Learn Java".length()); // 10 } }
In the above program, the length of the 'Java\n' string is5Instead of6This is because '\n' in Java is a single character (newline).