Java Array Length

Description

You can refer to the length of the array - the number of elements it contains - using length, a data member of the array object.

Syntax

Array size, arrayName.length, holds its length.

Example

The following code outputs the length of each array by using its length property.


public class Main {
  public static void main(String args[]) {
    int a1[] = new int[10];
    int a2[] = {1, 2, 3, 4, 5};
    int a3[] = {4, 3, 2, 1};
    System.out.println("length of a1 is " + a1.length);
    System.out.println("length of a2 is " + a2.length);
    System.out.println("length of a3 is " + a3.length);
  }//  w w  w. j a  v a 2s  . c om
}

This program displays the following output:





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures