Java Array – length

Previous
Next

length:

  • It is non static variable belongs to array object.
  • We call ‘length’ variable on array object.
  • It returns the length of array in int format.
class Demo 
{
	public static void main(String[] args) 
	{
		int arr[ ] = new int[5];
		int len = arr.length;
		System.out.println("Length is : " + len);
	}
}

Read the size of array:

import java.util.Scanner;
class Demo 
{
	static int arr[];  // declaration
	static
	{
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter size of array : ");
		int n = scan.nextInt();
		arr = new int[n];
	}
	public static void main(String[] args) 
	{
		int len = arr.length;
		System.out.println("Length is : " + len);
	}
}
Previous
Next

Add Comment

Courses Enquiry Form