Find Length of String in Java
Different ways to find the length of a string in Java.
Last updated: 11 January 2026
Method 1: Using length() method
Standard String method.
Main.javaRun in Compiler →
public class StringLength {
public static void main(String[] args) {
String str = "Hello Java";
System.out.println("String: " + str);
System.out.println("Length: " + str.length());
}
}Output:
String: Hello Java Length: 10
Explanation
The length() method returns the number of characters in the string.
Method 2: Without length() method
Using char array loop.
Main.javaRun in Compiler →
public class StringLength {
public static void main(String[] args) {
String str = "Hello Java";
int length = 0;
for(char c : str.toCharArray()) {
length++;
}
System.out.println("Length: " + length);
}
}Output:
Length: 10
Explanation
Iterate through characters and increment counter.
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler