Java Array Length

In this chapter you will learn:

  1. What is Java Array Length
  2. Syntax for Java Array Length
  3. Example - 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.  jav a  2s  .c  om
}

This program displays the following output:

Next chapter...

What you will learn in the next chapter:

  1. How to Initialize Java Arrays
  2. Syntax to Initialize Arrays
  3. Example - How to initialize an array during the declaration
Home »
  Java Tutorial »
    Java Langauge »
      Java Array
Java Array
Java Array Variables
Java Array Initial Values
Java Array Length
Java Initialize Arrays
Java Array Index
Java Array for each loop
Java Multidimensional Arrays