Home
Java
Array Length
This page was last reviewed on Dec 1, 2023.
Dot Net Perls
Length, array. An array may be empty—it may have zero elements. But it still has an element count (a size). We access length, an int, on all array instances.
Array
Shows an array
Loops, optimizations. Accessing length is fast. And in loops, using length directly, not a local variable, may lead to better performance.
Example program. Let us use an array's length in a simple context. We find that the array here has three elements. And its last index is derived from its length.
Warning If an array has zero elements, you cannot get the last index by subtracting one. We must check this case.
Shows an array
public class Program { public static void main(String[] args) { String[] array = { "cat", "apple", "frog" }; // Display length of the array. System.out.println(array.length); // Display first and last elements. System.out.println(array[0]); System.out.println(array[array.length - 1]); } }
3 cat frog
Length, loop boundary. This is a basic example, and you already know how to loop over a simple array. An array's length makes a good upper boundary for a loop.
for
public class Program { public static void main(String[] args) { int[] numbers = { 5, 10, 15, 20 }; // Loop over numbers using length. for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } } }
5 10 15 20
Benchmark, length versus local. Suppose we need to check the array length in a program many times, and it does not change. This is not in a loop.
Version 1 Here we access the array's length each time in a hot inner loop. The length never changes.
Version 2 In this version of the code we use a cached "length" local instead of accessing length each time.
Result Checking a local variable that copies the value of an array's length is faster.
Warning This optimization does not work in a loop boundary. The Java runtime applies special optimizations in loops.
public class Program { public static void main(String[] args) { int[] array = new int[1000]; int length = array.length; long t1 = System.currentTimeMillis(); // Version 1: check array length. for (int i = 0; i < 10000000; i++) { for (int j = 0; j < 100; j++) { if (array.length != 1000) { System.out.println(false); } } } long t2 = System.currentTimeMillis(); // Version 2: check length cached value. for (int i = 0; i < 10000000; i++) { for (int j = 0; j < 100; j++) { if (length != 1000) { System.out.println(false); } } } long t3 = System.currentTimeMillis(); // ... Timings. System.out.println(t2 - t1); System.out.println(t3 - t2); } }
14 ms, check array.length 9 ms, check local variable
Benchmark, length in loop. Now we see how using length in a for-loop boundary condition helps performance. First this benchmark allocates, and loops over, an array.
Version 1 This code loops over a 100,000 element array and accesses array.length in the for-loop limit.
Version 2 This version uses a local variable instead of array.length. In the loop it accesses the array.
Result The loop that accesses the array length directly is faster. The compiler is optimizing bounds-checking in the loop body.
public class Program { public static void main(String[] args) { int[] array = new int[100000]; int length = array.length; // Access all elements in memory (prime the cache). for (int v = 0; v < array.length; v++) { if (array[v] != 0) { System.out.println(false); } } long t1 = System.currentTimeMillis(); // Version 1: use array length in for-loop maximum. for (int i = 0; i < 30000000; i++) { for (int j = 0; j < 100; j++) { int count = 0; for (int v = 0; v < array.length; v++) { count++; } if (count == 0) { System.out.println(0); } } } long t2 = System.currentTimeMillis(); // Version 2: use local variable as for-loop maximum. for (int i = 0; i < 30000000; i++) { for (int j = 0; j < 100; j++) { int count = 0; for (int v = 0; v < length; v++) { count++; } if (count == 0) { System.out.println(0); } } } long t3 = System.currentTimeMillis(); // ... Timings. System.out.println(t2 - t1); System.out.println(t3 - t2); } }
3114 ms, loop with length max 6785 ms, loop with local variable max
Summary. Array lengths are often used in Java programs. We can access last element in an array with length. And length can be used in a for-loop to iterate over all the indexes in an array.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 1, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.