How to determine length or size of an Array in Java?

There is no size() method available with the array. But there is a length field available in the array that can be used to find the length or size of the array.

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.

Examples:



int size = arr[].length;

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

Below is the illustration of how to get the length of array[] in Java using length variable:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// how to get the length of the array
  
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);
    }
}

chevron_right


Output:

The size of the array is 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// how to get the length of the array
  
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("The size of "
                           + "the array is "
                           + str.length);
    }
}

chevron_right


Output:

The size of the array is 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.