Open In App
Related Articles

length vs length() in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

array.length: length is a final variable applicable for arrays. With the help of the length variable, we can obtain the size of the array. 

string.length() : length() method is a final method which is applicable for string objects. The length() method returns the number of characters present in the string. 

length vs length()

1. The length variable is applicable to an array but not for string objects whereas the length() method is applicable for string objects but not for arrays.

2. Examples:

// length can be used for int[], double[], String[] 
// to know the length of the arrays.

// length() can be used for String, StringBuilder, etc 
// String class related Objects to know the length of the String

3. To directly access a field member of an array we can use .length; whereas .length() invokes a method to access a field member.

Example:  

JAVA




// Java program to illustrate the
// concept of length
// and length()
public class Test {
    public static void main(String[] args)
    {
        // Here array is the array name of int type
        int[] array = new int[4];
        System.out.println("The size of the array is "
                           + array.length);
 
        // Here str is a string object
        String str = "GeeksforGeeks";
        System.out.println("The size of the String is "
                           + str.length());
    }
}


Output

The size of the array is 4
The size of the String is 13

Practice Questions based on the concept of length vs length()

Let’s have a look at the output of the following programs:

  • What will be the output of the following program?

JAVA




public class Test {
    public static void main(String[] args)
    {
        // Here str is the array name of String type.
        String[] str = { "GEEKS", "FOR", "GEEKS" };
        System.out.println(str.length);
    }
}


Output

3

Explanation: Here the str is an array of type string and that’s why str.length is used to find its length.  

  • What will be the output of the following program? 
     

JAVA




public class Test {
    public static void main(String[] args)
    {
        // Here str[0] pointing to a string i.e. GEEKS
        String[] str = { "GEEKS", "FOR", "GEEKS" };
        System.out.println(str.length());
    }
}


Output: 

error: cannot find symbol
symbol: method length()
location: variable str of type String[]

Explanation: Here the str is an array of type string and that’s why str.length() CANNOT be used to find its length.  

  • What will be the output of the following program?

JAVA




public class Test {
    public static void main(String[] args)
    {
        // Here str[0] pointing to String i.e. GEEKS
        String[] str = { "GEEKS", "FOR", "GEEKS" };
        System.out.println(str[0].length());
    }
}


Output

5

Explanation: Here str[0] pointing to String i.e. GEEKS and thus can be accessed using .length()
 
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials