The array length property tells you how many items are in the array. It helps you know the size of the array so you can use it safely.
Array length property in Java
public class ArrayLengthExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; int lengthOfArray = numbers.length; System.out.println("Length of array: " + lengthOfArray); } }
The length property is not a method, so it does not use parentheses.
It works only with arrays, not with other collections like ArrayList.
int[] emptyArray = {}; System.out.println(emptyArray.length);
int[] singleElementArray = {10}; System.out.println(singleElementArray.length);
int[] numbers = {5, 10, 15}; System.out.println(numbers.length);
This program creates an array of scores, prints all the scores, and then prints the length of the array using the length property.
public class ArrayLengthDemo { public static void main(String[] args) { int[] scores = {85, 90, 78, 92, 88}; System.out.println("Array before processing:"); for (int index = 0; index < scores.length; index++) { System.out.print(scores[index] + " "); } System.out.println(); System.out.println("Length of the array: " + scores.length); } }
Accessing the length property is very fast, it takes constant time O(1).
The length property uses a small fixed amount of memory, so space complexity is O(1).
A common mistake is to try to use length() with arrays, but length is a property without parentheses.
Use length when you need the fixed size of an array. For dynamic collections, use size() method instead.
The length property tells you how many elements are in an array.
It is used without parentheses, unlike methods.
Knowing the length helps you avoid errors when accessing array elements.
