In Java, the array length is the number of elements that an array can hold. There is no predefined method to obtain the length of an array. We can find the array length in Java by using the array attribute length. We use this attribute with the array name.
Array length Attribute
Java provides an attribute length that determines the length of an array. Every array has an in-built length property whose value is the size of the array. Size implies the total number of elements that an array can contain. The length property can be invoked by using the dot (.) operator followed by the array name. We can find the length of int[], double[], String[], etc. For example:
- int[] arr=new int[5];
- int arrayLength=arr.length
In the above code snippet, arr is an array of type int that can hold 5 elements. The arrayLength is a variable that stores the length of an array. To find the length of the array, we have used the array name (arr) followed by the dot operator and length attribute, respectively. It determines the size of the array.

Note that length determines the maximum number of elements that the array can contain or the capacity of the array. It does not count the elements that are inserted into the array. That is, length returns the total size of the array. For arrays whose elements are initialized at the time of its creation, length and size are the same.
If we talk about the logical size, the index of the array, then simply int arrayLength=arr.length-1, because the array index starts from 0. So, the logical or array index will always be less than the actual size by 1.

Let’s find the length of the array through an example.
Example
- public class Main {
- public static void main(String[] args) {
- //defining an array of type int named num
- //The square bracket contains the length of an array
- int[] num = new int[10];
- //length is an Array attribute that determines the array length
- int arrayLength=num.length;
- //prints array length
- System.out.println(“The length of the array is: “+ arrayLength);
- }
- }
Compile and Run
Output:
The length of the array is: 10
Explanation
This Java code, which is part of the ArrayLengthExample1 class, shows how to find an array’s length. The syntax int[] num = new int[10]; is used in the main method to declare and initialise an array of type int with a length of 10. When an array is declared, its length is specified by the square brackets. The code then determines the length of the num array by using the length attribute, which is built into all Java arrays, and assigning the result to the variable arrayLength. Finally, the program prints out the length of the array using System.out.println, providing information about the number of elements the array can hold.
Example
- public class Main {
- public static void main(String[] args) {
- //Initialising an array of type String named country
- String[] country = { “India”, “Australia”, “Japan”, “USA”, “UAE”, “Canada”, “Brazil”};
- //length is an Array attribute that determines the array length
- int arrayLength=country.length;
- //prints array length
- System.out.println(“The size of the array is: ” + arrayLength);
- }
- }
Output:
The size of the array is: 7
Explanation
With a main method, the class ArrayLengthExample2 is defined in the Java code that is provided. Several country names are initialised into an array of type String called country inside the main procedure. Next, the code calculates the length of the nation array, or the total number of entries it may contain, using Java’s length attribute for arrays. The variable arrayLength is given the length. The array’s size and the number of elements it contains are finally printed by the programme using System.out.println. This code basically shows how to use Java’s length attribute to find the length of an array and how to output the length for reference.
Example
- public class Main {
- private static void LengthOfArray(String[] array) {
- //checks array is empty or not
- if (array == null) {
- //if the array is empty, prints the following statement
- System.out.println(“The array is empty, can’t be determined the length.”);
- }
- else {
- //The length attribute of the Array class determines the length of an array
- int arrayLength = array.length;
- //prints the array length
- System.out.println(“The length of the array is: “+arrayLength);
- }
- }
- public static void main(String[] args) {
- String[] fruits = { “Guava”, “Banana”, “Apple”, “Papaya”, “Melon”, “Strawberry”};
- String[] alphabets = { “m”, “p”, “k”, “l”, “t” };
- String[] numbers = { “12”, “25”, “63”, “84”, “90”, “11”, “54”};
- //passing a null value to the function
- LengthOfArray(null);
- //passing the fruits array to the function
- LengthOfArray(fruits);
- //passing the alphabet array to the function
- LengthOfArray(alphabets);
- //passing the numbers array to the function
- LengthOfArray(numbers);
- }
- }
Compile and Run
Output:
The array is empty, can't be determined the length.
The length of the array is: 6
The length of the array is: 5
The length of the array is: 7
Explanation
The Java code that is provided defines a class called Main, and in that class, there is a method called LengthOfArray that is used to find and report the length of a specified array of strings. The procedure initially determines if the supplied array is null or empty by looking at its value. It prints a message saying that the length cannot be computed if the array is empty. If not, it uses the Array class’s length attribute to determine the array’s length and displays it. LengthOfArray’s usefulness is demonstrated in the main method by passing several arrays, such as a null value, an array of fruits, alphabets, and numerals. The length of each array is output by the programme when it runs.
Iterating through the Array
Another method involves iterating through the array and counting the number of elements encountered. This approach provides more flexibility and control over the counting process.
Example
- public class Main {
- public static void main(String[] args) {
- int[] arr = new int[5];
- int count = 0;
- for (int element : arr) {
- count++;
- }
- int arrayLength = count;
- System.out.println(“The length of the array is: ” + arrayLength);
- }
- }
Compile and Run
Output:
The length of the array is: 5
Leave a Reply