Common Pitfalls in Java Arrays
Arrays are powerful, but beginners often encounter mistakes when using them. Knowing these common pitfalls will help you write bug-free and efficient code.
1. ArrayIndexOutOfBoundsException
Occurs when you access an index that doesn’t exist in the array.
public class OutOfBoundsExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]); // Error
}
}
2. Null Arrays
If an array reference points to null, any operation will throw a NullPointerException.
public class NullArrayExample {
public static void main(String[] args) {
int[] numbers = null;
System.out.println(numbers.length); // Error
}
}
int[] numbers = new int[5]; // Correct
3. Choosing the Wrong Array Size
Arrays in Java have fixed sizes. Declaring a size too small can cause errors, too large wastes memory.
int[] scores = new int[3]; // Can only store 3 elements
scores[3] = 50; // OutOfBounds
4. Arrays vs Collections
| Feature | Array | Collection (e.g., ArrayList) |
|---|---|---|
| Size | Fixed | Dynamic (can grow/shrink) |
| Type | Can store primitives | Stores objects only |
| Performance | Faster (less overhead) | Slightly slower (more flexible) |
| Methods | Limited (length, indexing) | Many utility methods (add, remove, sort, contains) |
Common mistake: Using arrays when a dynamic structure is needed, or using ArrayList when performance-critical.
5. Mixing Types
Arrays can only store a single data type. Trying to mix types leads to compilation errors.
Object[] arr = {1, "Java", true}; // Possible with Object array
int[] numbers = {1, "2"}; // Error
Tips to Avoid Common Pitfalls
- Always check array bounds.
- Initialize arrays before use.
- Use
numbers.lengthto control loops. - Choose arrays for fixed-size, primitive-heavy data.
- Prefer Collections for dynamic and object-heavy data.
- Avoid mixing incompatible types.
🏆 Top 5 Interview Questions – Common Pitfalls in Java Arrays
1. What causes ArrayIndexOutOfBoundsException?
Answer:
- Accessing an array element beyond its valid index range.
- Array indices start at 0 and end at length - 1.
Example:
int[] arr = {1,2,3};
System.out.println(arr[3]); // Error
2. What is the difference between null array and empty array?
Answer: Null array: Reference points to null, any access causes NullPointerException.
int[] arr = null;
System.out.println(arr.length); // Error
Empty array: Has valid length 0, safe to access .length.
int[] arr = new int[0];
System.out.println(arr.length); // 0
3. Why should we be careful with array size?
Answer:
- Arrays are fixed-size in Java.
- Too small →
ArrayIndexOutOfBoundsException. - Too large → Wasted memory.
Example:
int[] arr = new int[5];
arr[5] = 10; // Error
4. When should you use arrays vs collections?
Answer:
| Feature | Array | Collection (ArrayList) |
|---|---|---|
| Size | Fixed | Dynamic |
| Type | Primitives possible | Objects only |
| Methods | Limited | Many utility methods |
| Performance | Faster | Slightly slower |
Tip: Use arrays for performance-critical, fixed-size data; use collections for dynamic, object-heavy data.
5. Can arrays store multiple data types?
Answer:
- Primitive arrays: No, can store only one type.
- Object arrays: Can store multiple object types (e.g.,
Object[] arr = {1, "Java", true};)
