Course contentsShow
← Java
Lesson 240 of 2,8706. ArraysFree lesson

Array Length Property

Access the fixed size of an array using the length property: arr.length.

Array Length Property

What you'll learn: How to find out exactly how many elements an array can hold using its built-in length property.

The Fixed Size of Arrays

When you create an array in Java, its size is fixed and cannot change. But how do you find out how big an array actually is? Java provides every array with a special length property that tells you the number of elements the array can hold.

Accessing the Length Property

Unlike methods that require parentheses, length is a property (also called a field). You access it using the dot operator:

int[] numbers = new int[5];
int size = numbers.length;  // size is 5

Notice there are no parentheses after length — it's numbers.length, not numbers.length().

Why This Matters

The length property is incredibly useful when working with arrays:

int[] scores = {85, 92, 78, 90};
int totalElements = scores.length;  // totalElements is 4

// Safely access the last element
int lastScore = scores[scores.length - 1];  // scores[3] = 90

Think of it like this: If an array is a row of lockers, length is the total number of lockers in that row — whether they're empty or full doesn't matter.

Common Use Cases

You'll frequently use length to avoid going out of bounds or when iterating through arrays:

int[] data = {10, 20, 30};
for (int i = 0; i < data.length; i++) {
    // Process each element
}

Key Takeaway: Every array has a length property (not a method) that returns the fixed number of elements it can hold. Use array.length to access it.