Java Program to Find Length of an Array

In Java, the length of an array refers to the number of elements the array can hold. You can access this length using the .length property. This is a built-in property provided by Java for all arrays.

Fine Java Array Length Program

package com.javacodepoint.array.basic;

// Java program to Find Length of an Array
public class ArrayLengthExample {
	// main method
	public static void main(String[] args) {
		// Declare and initialize an array
		int[] numbers = { 10, 20, 30, 40, 50 };

		// Find the length of the array
		int length = numbers.length;

		// Print the length of the array
		System.out.println("The length of the array is: " + length);
	}
}

OUTPUT:

Code Explanation:

  1. Array Declaration and Initialization:
    • int[] numbers = {10, 20, 30, 40, 50};
      Here, we declare an array of integers with 5 elements.
  2. Accessing the Array Length:
    • int length = numbers.length;
      The .length property gives the total number of elements in the array.
  3. Printing the Length:
    • System.out.println("The length of the array is: " + length);
      This outputs the value of length.

Key Points:

  1. .length is a property for arrays, not a method. So, it doesn’t have parentheses (i.e., no .length()).
  2. The indexing of arrays starts at 0 but .length gives the total count of elements (not the highest index).

Read Also: Get Array Input | Find the Sum of an Array

Java logical programs list


Java Basic Programs

Java String Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *