Java - String length() Method



The Java String length() method is, used to retrieve the length of the string. A length is the number of characters presented in the string. The length() method does not accept any parameters, It does not throw any exception while retrieving the length of the string.

A string is an object that holds the sequence of the characters. The java.lang.string class represents the string in Java. The strings are immutable in Java their values cannot be changed after they are created.

Note − The length of the string is nothing but just a count of the characters that are present in the given string, it counts the white spaces as well along with the string characters.

Syntax

Following is the syntax of the Java String length() method −

public int length()

Parameters

  • It does not accept any parameter.

Return Value

This method returns the length of the sequence of characters represented by this object.

Example

If the given string is not null, the length() method returns the length of the string.

In the following program, we are creating a String literal with the value “HelloWorld”. Then, using the length() method, we are trying to retrieve the length of the current string.

import java.lang.*;
public class Length {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "HelloWorld";
      System.out.println("The given string is: " + str);
      
      //using the length method;
      int len = str.length();
      System.out.println("The lenght of the string is: " + len);
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: HelloWorld
The length of the string is: 10

Example

If the given string value is empty, this method returns 0.

In the following example, we create an object of the string class with an empty value. Using the length() method, we are trying to get the string length of the current string.

import java.lang.*;
public class Length {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = "";// empty string
      System.out.println("The given string is: " + str);
      
      //using the length method;
      int len = str.length();
      System.out.println("The lenght of the string is: " + len);
   }
}

Output

Following is the output of the above program −

The given string is an empty. 
The length of the string is: 0

Example

Using the length() method, we can find the larger string based on its length.

In this program, we are instantiating the string class with the value “java”. Then, we create a string literal with the value “point”. Using the length() method, we are trying to find the larger string based on string length.

import java.lang.*;
public class Length {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("java");
      System.out.println("The first string is: " + str);
      String str2 = "point";
      System.out.println("The second string is: " + str2);
      
      //using the length method;
      int len1 = str.length();
      int len2 = str2.length();
      System.out.println("The length of the strings is: " + len1 + " and " + len2) ;
      if(len1 > len2) {
         System.out.println("The first string is larger than the second string");
      } else {
         System.out.println("The first string is smaller than the second string");
      }
   }
}

Output

The above program, produces the following output −

The first string is: java
The second string is: point
The length of the strings is: 4 and 5
The first string is smaller than the second string

Example

If the given string value is null, the length() method throws the NullPointerException.

In the following program, we declare a string and initialize it with the null value. Using the length() method, we are trying to retrieve the string length. Since the given string value is null, this method throws an exception.

import java.lang.*;
public class Length {
   public static void main(String[] args) {
      try {
         
         //declare a string
         String str;
         
         //initialize it with null value
         str = null;
         System.out.println("The given string is: " + str);
         
         //using the length() method
         System.out.println("The length of the string is: " + str.length());
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: null
java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
	at com.tutorialspoint.String.Length.main(Length.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
java_lang_string.htm
Advertisements