How to Get the Length of an Array in Java
Get the length of a Java array with the .length field, including for multi-dimensional arrays.
How to Get the Length of an Array in Java
A Java array stores its size permanently, and you read that size through the length field. This is one of the most common operations you will do with arrays — to loop over them, find the last element, or check whether they hold any data at all. This chapter shows how length works, how it differs from String.length(), and how to measure multi-dimensional arrays.
Use the length field
Every array in Java exposes a public final int length field that holds the number of elements. You read it with no parentheses, because it is a field, not a method.
int[] scores = {88, 92, 75, 60, 100};
int size = scores.length; // 5
System.out.println(scores.length); // 5The value is fixed when the array is created and never changes — arrays in Java do not grow or shrink. If you need a resizable collection, reach for ArrayList instead, which exposes size() as a method. The length field also works the same way for arrays of any type: String[], double[], or even an array of objects.
Field vs. method: length vs. length()
A frequent source of confusion is mixing up arrays with strings. An array uses the field length; a String uses the method length(). Writing the wrong one will not compile.
int[] numbers = {1, 2, 3};
String text = "hello";
System.out.println(numbers.length); // field, no () -> 3
System.out.println(text.length()); // method, with () -> 5| Type | How to get the size | Parentheses? |
|---|---|---|
Array (int[], String[], ...) | array.length | No |
String | str.length() | Yes |
ArrayList, HashMap, ... | collection.size() | Yes |
A simple rule: arrays are the only one of the three that uses a bare field. Everything else uses a method call.
Handle empty and last-element cases
An array created with size 0 is a real, non-null object whose length is 0. That is different from a null reference, which has no length at all — reading .length on null throws NullPointerException. To use the size safely, iterate from 0 up to length - 1, since the last valid index is always one less than the length.
int[] empty = new int[0];
System.out.println(empty.length); // 0, not an error
int[] data = {10, 20, 30};
int last = data[data.length - 1]; // 30
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]);
}If an array might be null, guard it first: if (data != null && data.length > 0).
Measure multi-dimensional arrays
A two-dimensional array in Java is really an array of arrays. The outer length gives the number of rows, and each row has its own length for that row's column count. Rows can even have different lengths (a jagged array), so always read each row's length individually rather than assuming a uniform width.
int[][] grid = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};
System.out.println(grid.length); // 3 rows
System.out.println(grid[0].length); // 3 columns in row 0
System.out.println(grid[2].length); // 4 columns in row 2To count every element, sum each row's length instead of multiplying rows by columns — that shortcut only works for perfectly rectangular grids.
Worked example
The program below pulls all of these ideas together: a flat array, the field-vs-method distinction, an empty array, last-element access, and a jagged 2D array whose total size is computed by summing row lengths.
What to take from the run:
scores.length = 5confirmslengthis read as a bare field with no parentheses and returns the element count.word.length() = 5shows theStringsize comes from the methodlength(), a different mechanism from the array field.empty.length = 0proves a zero-length array is a valid object, notnull, and reading its length is safe.last element = 100demonstrates that the final index islength - 1, since indexing starts at0.grid.length (rows) = 3whilegrid[2].lengthis4, showing the outer length counts rows and each row carries its own length — summed tototal elements in grid = 9.
Practice
In Java, how do you correctly get the number of elements in an int array named 'data'?