In AP Computer Science A, array.length is a public final field (not a method, so no parentheses) that gives the number of elements an array was created to hold; since indexes run from 0 to length - 1, it sets the upper bound for every array traversal loop.
Every Java array comes with a built-in field called length that tells you how many elements the array holds. You access it with dot notation and no parentheses, like nums.length. That's because it's a field, not a method. The value is fixed the moment the array is created. If you write int[] scores = new int[10];, then scores.length is 10 forever, even if you never store meaningful values in some of those slots.
The single most useful thing to burn into your brain is the relationship between length and indexes. An array of length 10 has valid indexes 0 through 9. So arr.length itself is NOT a valid index. The last element lives at arr[arr.length - 1]. Almost every array bug on the AP exam, and in your own code, comes from forgetting that gap of one.
Arrays are a core data structure in AP CSA (Unit 6 in the CED), and array.length is how you write correct traversal code. Every standard array loop uses it in the condition, like for (int i = 0; i < arr.length; i++). Change that < to <= and you walk one step past the end of the array, which throws an ArrayIndexOutOfBoundsException at runtime. The exam loves testing exactly that distinction. The concept also scales up. In Unit 8, a 2D array uses arr.length for the number of rows and arr[0].length for the number of columns, and in Unit 7 you have to remember that ArrayLists use .size() instead. Knowing which container uses which is a free point waiting to be collected.
Keep studying AP Computer Science A Unit 4
ArrayIndexOutOfBoundsException (Unit 6)
This exception is what happens when you ignore array.length. Accessing arr[arr.length] is the classic trigger because the last valid index is length - 1, not length. Practice questions about preventing this exception are really asking whether your loop condition uses < arr.length instead of <=.
Index (Unit 6)
Length counts elements; an index names a position. They're off by one on purpose. A length of 5 means indexes 0, 1, 2, 3, 4. Whenever you see arr.length - 1 in code, read it as 'the last index.'
Iteration (Units 4 & 6)
array.length is the bridge between loops and arrays. The standard traversal pattern uses i < arr.length as the loop condition, and nested loops that check for things like duplicate elements use it for both the outer and inner bounds.
Array of objects (Unit 6)
length works the same whether the array holds ints or objects. A new array of objects has the right length immediately, but every slot starts as null, so the length tells you capacity, not how many real objects you've actually stored.
array.length shows up constantly in multiple-choice code-reading questions. Common stems ask you to identify the correct way to get the number of elements in a 1D array (answer: the length field, no parentheses), to spot which loop condition causes an off-by-one error (using <= arr.length instead of < arr.length), or to explain how to prevent an ArrayIndexOutOfBoundsException while iterating. It also hides inside algorithm questions, like determining whether an array contains duplicates, where the nested loop bounds depend on arr.length. On the FRQs, any array or 2D array method you write will almost certainly need arr.length in a loop condition. Using arr.length() with parentheses or arr.size() in an FRQ is a real, point-costing error, so practice writing the bare field until it's automatic.
Three containers, three different ways to count. Arrays use the field arr.length with no parentheses. Strings use the method str.length() with parentheses. ArrayLists use list.size(). Mixing these up causes compile errors in your own code and is a favorite trap in MCQ answer choices. A quick memory hook: arrays are the primitive-feeling one, so they get a plain field instead of a method call.
array.length is a field, not a method, so you write arr.length with no parentheses.
An array's length is fixed when the array is created with new and never changes afterward.
Valid indexes run from 0 to arr.length - 1, so arr[arr.length] always throws an ArrayIndexOutOfBoundsException.
The standard traversal loop condition is i < arr.length; writing i <= arr.length is the classic off-by-one error.
For 2D arrays, arr.length gives the number of rows and arr[0].length gives the number of columns.
Arrays use .length, Strings use .length(), and ArrayLists use .size(), and the AP exam tests whether you keep them straight.
It's the built-in field on every Java array that stores how many elements the array holds. For int[] nums = new int[8], nums.length is 8, and the valid indexes are 0 through 7.
No, it's a public final field, so you write arr.length with no parentheses. Writing arr.length() causes a compile error, and it's a deliberate trap in MCQ answer choices.
Arrays use the field arr.length, Strings use the method str.length() with parentheses, and ArrayLists use the method list.size(). All three return an int count, but the syntax is different for each, and the exam tests all three.
Because indexes start at 0, an array of length n has its last element at index n - 1. arr[arr.length] points one slot past the end of the array, so Java throws an ArrayIndexOutOfBoundsException at runtime.
No. The length is set when the array is created and never changes. Overwriting a value or setting an object slot to null doesn't shrink the array; if you need a resizable container, that's what ArrayList is for.