Java String length()
The java string length() method length of the string. It returns count of total number of characters. The length of java string is same as the unicode code units of the string.
Internal implementation
public int length() {
return value.length;
}
Signature
The signature of the string length() method is given below:
public int length()
Specified by
CharSequence interface
Returns
length of characters
Java String length() method example
public class LengthExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string
System.out.println("string length is: "+s2.length());//6 is the length of python string
}}
Test it Now
string length is: 10
string length is: 6
Java String length() Method Example 2
public class LengthExample2 {
public static void main(String[] args) {
String str = "Javatpoint";
if(str.length()>0) {
System.out.println("String is not empty and length is: "+str.length());
}
str = "";
if(str.length()==0) {
System.out.println("String is empty now: "+str.length());
}
}
}
String is not empty and length is: 10
String is empty now: 0