Easy Tutorial
❮ Java String Hashcode Collection Minmax ❯

Java String length() Method

Java String Class


The length() method is used to return the length of the string.

The length of an empty string returns 0.

Syntax

public int length()

Parameters

Return Value

Returns the length of the string.

Example

The following example demonstrates the use of the length() method:

Example

public class Test {
        public static void main(String args[]) {
                String Str1 = new String("www.tutorialpro.org");
                String Str2 = new String("tutorialpro");

                System.out.print("Length of string Str1: ");
                System.out.println(Str1.length());
                System.out.print("Length of string Str2: ");
                System.out.println(Str2.length());
        }
}

The output of the above program is:

Length of string Str1: 14
Length of string Str2: 6

Java String Class

❮ Java String Hashcode Collection Minmax ❯