Open In App

How to Find Length or Size of an Array in Java?

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.

In this article, we will learn:

  • How to find the length of an array using the length property
  • Difference between length, length() and size() methods
  • Various approaches to find the array length, including loops and Stream API

Different Ways to Find the Length or Size of an Array

1. Using the length Property (Best and Easiest Way)

The length property is the most common way to find the size of an array in Java. It gives the exact number of elements present in the array.

Example: This example demonstrates how to find the length (size) of an array using the length property.

Java
// Finding the length of an Array
public class Geeks {
  
    public static void main(String[] args)
    {
        // Here arr is the
        // array name of int type
        int[] arr = new int[4];

        System.out.println("The size of the Array is " + arr.length);
    }
}

Output
The size of the Array is 4

Time complexity: O(1) constant time

Note:

  • length is a property not a method.
  • length property works for all array types (int[] , String[], double[]) etc.


2. Finding Array Length Using a For Loop (Manual Counting)

When iterating over an array, we can also determine its size manually.

Example: This example, demonstrates how to use for each loop to calculate the length of an array.

Java
// Java program to demonstrate for loop
// to calculate length of all type of Arrays
import java.util.*;

public class Geeks {
    public static void main(String[] arg) {

        int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
      	int c = 0;
        
      	for (int i : arr)
            c++;
        
      	System.out.println("The Size of the array is " + c);
        
    }
}

Output
The Size of the array is 7

Time Complexity: O(n) slower than length

Note: This approach is not recommended in real world code.


3. Finding Array Length Using Java 8 Stream API

Stream API was introduced in Java 8 and because of it we can perform various operations on arrays using functional programming. The Stream class provide count() method which is used to count the number of elements in an array.

Example: This example, demonstrates using Arrays.stream() with count() to calculate the length of an array.

Java
// Java program to demonstrate Stream.count()
// method to calculate length of an array
import java.util.*;

public class Geeks {
    public static void main(String[] argv)
    {

      	// Creating Array and Populating them
        int[] arr = { 1, 2, 3, 4, 5 };

        // calculating the length of the arrays
        long e = Arrays.stream(arr).count();
    
        // print the length of the arrays
        System.out.println("The size of the array is " + e);
    }
}

Output
The size of the array is 5

Time Complexity: O(n)

Note: Avoid Arrays.stream(arr).count() for arrays because it forces unnecessary iteration.


length vs length() vs size()

The difference between length, length() and size() is listed below:

Feature

Used For

Example

length

It is used for arrays to calculate the number of elements

arr.length

length()

It is used for strings to calculate the number of characters in the string

s.length()

size()

It is used for collections like ArrayList or List to calculate the number of elements

list.size()



Next Article
Practice Tags :

Similar Reads