ArrayIndexOutOfBoundsException in Java
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
ArrayIndexOutOfBoundsException happens when you try to access an array element using an invalid index.
Arrays in Java are zero-indexed, which means if you have an array of length 5, the valid indices are 0, 1, 2, 3, and 4. Trying to access index 5 or any negative index throws this exception. The most common cause is off-by-one errors in loops.
Freshers often write for (int i equals 0; i less-than-or-equals array dot length; i plus plus), which seems logical but fails. When i reaches the array length, that's one past the last valid index, so the exception is thrown. The correct condition is i less-than array dot length.
Another common scenario is when you calculate an index from user input. Say a user selects item number 3 from a menu. If you directly use that number as an array index without checking if it's within bounds, you risk crashing.
You should validate that the index is at least 0 and less than the array length before accessing. The modern approach is to avoid raw arrays when possible. In most application code, use ArrayList instead.
It has a get method that throws IndexOutOfBoundsException with a clearer message telling you what went wrong. Even better, use enhanced for-loops when you need to process every element. That eliminates index management completely, so you can't make the mistake.
In REST APIs, if you're accepting an array index as a path variable, always validate it before using it to look up data. Return a 400 Bad Request if it's out of bounds rather than letting the exception crash your application.
ArrayIndexOutOfBoundsException in Java
Direct Answer
ArrayIndexOutOfBoundsException occurs when you try to access an array with an index that's negative or greater than or equal to the array's length. Java arrays are zero-indexed, so valid indices run from 0 to length minus 1.
Code Example
public class ArrayIndexExample {
public static void main(String[] args) {
String[] products = {"Laptop", "Mouse", "Keyboard"};
// WRONG: off-by-one error, length is 3 but valid indices are 0-2
// for (int i = 0; i <= products.length; i++) {
// System.out.println(products[i]); // throws exception at i=3
// }
// CORRECT: use < instead of <=
for (int i = 0; i < products.length; i++) {
System.out.println(products[i]);
}
// SAFER: enhanced for-loop eliminates index management
for (var product : products) {
System.out.println(product);
}
// Validating calculated indices
int userIndex = getUserInput(); // might be anything
if (userIndex >= 0 && userIndex < products.length) {
System.out.println("Selected: " + products[userIndex]);
} else {
System.out.println("Invalid selection");
}
}
private static int getUserInput() { return 5; } // example
}
Common Mistakes & Pitfalls
- Using <= in loop conditions: Writing for (int i = 0; i <= array.length; i++) is the classic mistake. The last valid index is length minus 1, so this always crashes on the final iteration.
- Confusing length with size: Arrays use .length (a field), but ArrayList uses .size() (a method). Mixing these up causes compilation errors or logic bugs.
- Not validating calculated indices: When you compute an index from user input or business logic, freshers forget to check if it's within bounds before accessing the array, causing crashes in production.
Check your understanding
Answer these without looking back at the article.
- What is the difference between ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException?
- How does ArrayList differ from an array in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.