Array Java Programs

Print Length of an Array

In this tutorial, we will learn to write a 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 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.

Print Length of an Array in Java

public class ArrayExample {

	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);
	}
}

Explanation

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

Output

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

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs