Using Array Length in Java

To get the length of an array in Java, use the arrayName.length property. It returns the number of elements in the array as an int. The length of an array is fixed when the array is created and cannot be changed. For a list such as ArrayList, use the size() method instead. In this Java Array Length Example, we get the length of an array of five integers. Click Execute to run the Java Array Length Example online and see the result.
Using Array Length in Java Execute
public class ArrayLengthDemo {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        System.out.println("Length of numbers array: " + numbers.length);  // Outputs: "Length of numbers array: 5"
     }
}

Java Array length Syntax

length is a public final field that every array has. It holds an int and is read without parentheses.

Java Array length Syntax
arrayName.length

What is the difference between length, length() and size()?

Arrays have the length field. A String has the length() method, which returns the number of characters. Collections such as ArrayList have the size() method, which returns the number of elements they hold right now and grows as you add more.

Java Array Length Examples

The following are examples of getting the length of an array in Java. You can run any of these examples in the online Java code runner and change the arrays to see the length change.

Get the length of an array

length counts every element, including those that still hold the default value. An array created with new int[10] has a length of 10 before any value is assigned.

Java Array Length Example
public class LengthExample {
    public static void main(String[] args) {
        String[] languages = {"Java", "Python", "PHP"};
        int[] scores = new int[10];
        int[] empty = new int[0];
        System.out.println(languages.length);
        System.out.println(scores.length);
        System.out.println(empty.length);
    }
}

// output:
// 3
// 10
// 0

Loop through an array with length

Indexes start at 0, so the last element is at length - 1. Use i < length as the loop condition.

Java Array Length in For Loop Example
public class LoopLengthExample {
    public static void main(String[] args) {
        int[] numbers = {4, 8, 15};
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(i + ": " + numbers[i]);
        }
        System.out.println("Last: " + numbers[numbers.length - 1]);
    }
}

// output:
// 0: 4
// 1: 8
// 2: 15
// Last: 15

Get the length of a 2D array

A two-dimensional array is an array of arrays. matrix.length returns the number of rows, and matrix[0].length returns the number of elements in the first row. Rows can have different lengths.

Java 2D Array Length Example
public class MatrixLengthExample {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6}
        };
        int[][] jagged = { {1}, {2, 3}, {4, 5, 6} };
        System.out.println("Rows: " + matrix.length);
        System.out.println("Columns: " + matrix[0].length);
        System.out.println(jagged[0].length + " " + jagged[2].length);
    }
}

// output:
// Rows: 2
// Columns: 3
// 1 3

Compare length, length() and size()

The example stores the same word as a char array, as a String and as an element of a List.

Java length vs length() vs size() Example
import java.util.ArrayList;
import java.util.List;

public class LengthSizeExample {
    public static void main(String[] args) {
        char[] letters = {'J', 'a', 'v', 'a'};
        String word = "Java";
        List<String> words = new ArrayList<>();
        words.add(word);
        System.out.println(letters.length);
        System.out.println(word.length());
        System.out.println(words.size());
    }
}

// output:
// 4
// 4
// 1

See also

Updated: