Java Array Length: Finding the Size of an Array

Getting the length of an array in Java is done with the .length property β€” not a method. It's a tiny detail that confuses many beginners because it's different from String.length() and List.size(), which are methods.

Basic usage

int[] numbers = { 10, 20, 30, 40, 50 };
int n = numbers.length;   // 5 β€” no parentheses

String[] names = new String[100];
int size = names.length;  // 100 β€” size of the array, not count of non-null entries

The .length property returns the capacity of the array, i.e. the number of slots allocated β€” not the number of non-null elements.

.length vs .length() vs .size()

TypeSyntaxNotes
Arrayarr.lengthField access, no parentheses
Strings.length()Method call, UTF-16 code units
Collectionlist.size()Method call on Collection interface

Why the inconsistency? Arrays are built into the language; collections and Strings are classes. The array length is a read-only field exposed directly by the JVM.

Multi-dimensional arrays

A 2D array in Java is technically an array of arrays. The outer .length gives the number of rows; the inner .length gives the size of a row:

int[][] grid = {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 },
    { 10, 11, 12 }
};

System.out.println(grid.length);      // 4 rows
System.out.println(grid[0].length);   // 3 columns in row 0

for (int row = 0; row < grid.length; row++) {
    for (int col = 0; col < grid[row].length; col++) {
        System.out.print(grid[row][col] + " ");
    }
    System.out.println();
}

Rows in a 2D array can have different lengths β€” Java allows jagged arrays:

int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2, 3};
jagged[2] = new int[]{4, 5};

System.out.println(jagged[1].length); // 3

Length is fixed at creation

Array size is chosen when the array is created and cannot change afterwards. To "grow" an array, you must allocate a new, larger one and copy:

int[] bigger = java.util.Arrays.copyOf(numbers, numbers.length * 2);

Or, better, use ArrayList which handles resizing for you.

Null and empty array

int[] empty = new int[0];
System.out.println(empty.length); // 0 β€” valid

int[] nope = null;
int n = nope.length; // ❌ NullPointerException

Always check the reference first or use a utility:

int safeLength = (arr != null) ? arr.length : 0;
// Or, from java.util.Arrays
boolean empty = arr == null || arr.length == 0;

Counting non-null entries

If you only want to count real elements in a partially filled array, you have to loop or use Streams:

String[] names = new String[100];
names[0] = "Alice";
names[1] = "Bob";

long filled = java.util.Arrays.stream(names)
                             .filter(java.util.Objects::nonNull)
                             .count();
// filled == 2, names.length == 100

Length in a stream or enhanced for

In a for-each loop the length is used internally but not exposed:

for (int n : numbers) {
    System.out.println(n);
}

If you need the index, use a classic for:

for (int i = 0; i < numbers.length; i++) {
    System.out.println(i + ": " + numbers[i]);
}

Maximum array length

A Java array can hold up to Integer.MAX_VALUE - 8 elements (~2.1 billion). In practice, memory limits you long before that.

Common mistakes

  • Writing arr.length() β€” compile error, length is a field
  • Writing arr.size() β€” arrays don't have size()
  • Reading .length on a null array
  • Confusing allocated length with populated length

Once the three patterns β€” arr.length, str.length(), list.size() β€” are memorized, you'll never mix them up again.