Google News
logo
Java program to print number of Elements in an Array
In the following example of Java program to print the number of elements in an array :
Program :
public class ArrayLength {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int len = arr.length;
        System.out.println("The number of elements in the array is: " + len);
    }
}
Output :
The number of elements in the array is: 5
In this program, we first declare an integer array arr and initialize it with the values {1, 2, 3, 4, 5}. We then use the length property of the array to get the number of elements in the array and store it in an integer variable len.

Finally, we print the number of elements to the console using System.out.println().