string-length-java

string length java

Java String length()

The java string length() method length of the string. It returns tally of complete number of characters. The length of java string is same as the unicode code units of the string.

Internal implementation

  1. public int length() {
  2.       return value.length;
  3.   }

Signature

The signature of the string length() method is given below:

  1. public int length()

Specified by

CharSequence interface

Returns

length of characters

Java String length() method example

  1. public class LengthExample{
  2. public static void main(String args[]){
  3. String s1=“javatpoint”;
  4. String s2=“python”;
  5. System.out.println(“string length is: “+s1.length());//10 is the length of javatpoint string
  6. System.out.println(“string length is: “+s2.length());//6 is the length of python string
  7. }}
string length is: 10
string length is: 6

Java String length() Method Example 2

  1. public class LengthExample2 {
  2.     public static void main(String[] args) {
  3.         String str = “Javatpoint”;
  4.         if(str.length()>0) {
  5.             System.out.println(“String is not empty and length is: “+str.length());
  6.         }
  7.         str = “”;
  8.         if(str.length()==0) {
  9.             System.out.println(“String is empty now: “+str.length());
  10.         }
  11.     }
  12. }
String is not empty and length is: 10
String is empty now: 0