Java Program to Print Length of an Array

In this tutorial, we will learn writing java program to create an array and print the length of the array. Our program will return the size of the given array.

For example

Case 1: if the given the array is {1, 2, 3, 4}.

           The output should be 4.

Case 2: if the given array  is {9, 8, 7, 6, 5}.                                    

The output should be 5.

Java Program to Print Length of an Array

import java.util.*;  
public class Main
{
	public static void main(String[] args) {
		int arr[] = {2,4,6,8,9,4};
		System.out.println("Java Program to calculate length of array ");
        System.out.println("The size of the array arr is = "+ arr.length);
	}
}

Output

Java Program to calculate length of array 
The size of the array arr is = 6

Explanation

For the given array {2, 4, 6, 8, 9, 4}, the arr.length() function will calculates the length of the array, the arr.length() function will return the length of array in ‘int’ data type.

What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now