Home >>Java String Methods >Java String length() Method

Java String length() Method

Java String length() Method

Java length() method in Java String is used to returns the length (empty string is 0) of a specified string.

Syntax

public int length()

Parameters

None

Technical Details

Returns - It is used to return An int value which represents the length of the string

Java String length() Method Example 1


public class Lengthexample1 
{
  public static void main(String[] args) 
  {
    String Name = "HELLOWORLD";
    System.out.println(Name.length());
  }
}

Output:
10

Java String length() Method Example 2


public class Lengthexample2 
{  
    public static void main(String[] args) 
	{  
        String strln1 = "Phptpoint";  
        if(strln1.length()>0) 
		{  
            System.out.println("String is not empty and length is: "+strln1.length());  
        }  
        strln1 = "";  
        if(strln1.length()==0) 
		{  
            System.out.println("String is empty now: "+strln1.length());  
        }  
    }  
}

Output:
String is not empty and length is: 9
String is empty now: 0