ArrayIndexOutOfBoundsException in Java
Fast recall
Quick revision
Core answer
Understanding of zero-based array indexing and the classic off-by-one error that trips up beginners
Using arr[arr.length] thinking it's the last element, when valid indices are 0 to length-1
Explain why enhanced for loops eliminate this error entirely and mention the off-by-one trap explicitly
Interview-ready guide
Interview answer
ArrayIndexOutOfBoundsException is thrown when you try to access an array element using an index that doesn't exist.
Arrays in Java are zero-based, so if you've got an array with five elements, the valid indices are zero through four. The first element is at index zero and the last element is at index length minus one. The exception happens when you use a negative index or an index greater than or equal to the array's length.
The classic mistake freshers make is the off-by-one error. length, but that's actually one past the end. If your array has length five, arr[5] throws the exception because valid indices only go up to four.
To avoid this, you've got three approaches. First, use enhanced for loops whenever possible. They eliminate the index variable completely, so you can't mess it up.
Just iterate over each element directly. Second, if you need the index for something, use a traditional for loop but make sure your condition is i less than length, not less than or equal. That's where the mistake happens.
Third, when accepting an index from external input, do explicit bounds checking. Verify it's not negative and it's less than the array length before using it. For getting the last element specifically, use length minus one and check that length is greater than zero first, otherwise you'll get a negative index on empty arrays.
ArrayIndexOutOfBoundsException in Java
Direct Answer
ArrayIndexOutOfBoundsException is thrown when you try to access an array using an index that's either negative or greater than or equal to the array's length. Valid indices range from 0 to length minus one. Prevention includes bounds checking, using enhanced for loops, and avoiding off-by-one errors.
Code Example
public class ArrayProcessor {
public void processOrders(String[] orderIds) {
// WRONG: classic off-by-one error
// for (int i = 0; i <= orderIds.length; i++) {
// System.out.println(orderIds[i]); // crashes at i == length
// }
// CORRECT: traditional loop with proper bounds
for (int i = 0; i < orderIds.length; i++) {
System.out.println(orderIds[i]);
}
// BETTER: enhanced for loop eliminates index entirely
for (var orderId : orderIds) {
System.out.println(orderId);
}
}
public String getLastOrder(String[] orders) {
if (orders.length == 0) {
return "No orders";
}
// Last element is at length - 1, not length
return orders[orders.length - 1];
}
public String safeGet(int[] values, int index) {
// Defensive bounds checking
if (index < 0 || index >= values.length) {
return "Index out of bounds: " + index;
}
return "Value: " + values[index];
}
}
Common Mistakes & Pitfalls
- Using arr.length as an index: Freshers write arr[arr.length] thinking it gets the last element, but valid indices stop at length minus one. An array with length 5 has no index 5. This is the classic off-by-one error.
- Loop condition i less than or equal to length: Writing i less than or equal to arr.length in a for loop causes the last iteration to access index length, which is out of bounds. The correct condition is i less than length.
- Forgetting empty array checks: Calculating arr[arr.length minus one] on an empty array where length is zero gives arr[-1], throwing the exception with a negative index. Always verify length is greater than zero before accessing elements positionally.
Check your understanding
Answer these without looking back at the article.
- What is the difference between ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException?
- What is IndexOutOfBoundsException?
- How does ArrayList prevent ArrayIndexOutOfBoundsException internally?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.