Array vs ArrayList in Java
Fast recall
Quick revision
Core answer
ArrayList is a resizable List backed by an array and stores reference types, so primitive values use wrappers such as Integer. Arrays fit fixed-size or primitive data; ArrayList fits a collection whose size changes and needs List operations. Choose from the required behavior, not a blanket performance rule.Whether you understand the fixed-size constraint of arrays vs dynamic resizing of ArrayList, and know when to choose one over the other.
Saying ArrayList is always better — arrays are faster for indexed access and are the right tool when the size is known upfront (e.g. a 12-month calendar, a 3-channel RGB value).
Mention that ArrayList internally uses an array and doubles its capacity when full — so it is not magic, just an array with automatic resize logic wrapped around it.
Interview-ready guide
Interview answer
I'd frame it as one decision: does the number of elements change while the program runs? An array locks its size the moment you create it — new int[5] is five slots forever — and it stores primitives directly, which keeps it compact and fast for indexed access. An ArrayList is built for the opposite case: it grows on demand, so you just call add() and it handles the resizing.
The point I'd make to show I actually understand it is that an `ArrayList` isn't magic — internally it's a plain array, and when it fills up it allocates a new one about 1.5 times larger, copies the elements across, and swaps it in.
The trade-offs fall out of that: it holds objects only, so an int gets boxed into an Integer with some overhead, and you read elements with get() and ask its length with size(), a method, rather than the length field — mixing those two up is a classic slip.
The reflex answer that ArrayList is just better is the trap; for a fixed count of primitives like a 12-month total, an array is smaller and faster. So my rule is simple — fixed size and primitives, use an array; a size that changes at runtime, use an `ArrayList`.
Array vs ArrayList in Java
Direct Answer
An array has a fixed length and can store either primitives or references. ArrayList<E> is a resizable List backed by an array and stores reference types, so primitive values use wrappers such as Integer. Arrays fit fixed-size or primitive data; ArrayList fits a collection whose size changes and needs List operations. Choose from the required behavior, not a blanket performance rule.
Code Example
Watching the same job done both ways makes the trade-off tangible: the array hits a wall at its fixed size, while the ArrayList just keeps accepting elements. Run it as a Java 17 console app.
import java.util.ArrayList;
public class ArrayVsArrayList {
public static void main(String[] args) {
// ARRAY — fixed size, direct primitive storage
int[] topScores = new int[3];
topScores[0] = 95;
topScores[1] = 88;
topScores[2] = 76;
// topScores[3] = 70; // compiles, but throws ArrayIndexOutOfBoundsException at runtime
System.out.println(topScores.length); // 3
// ARRAYLIST — dynamic, grows as needed
ArrayList<Integer> playerScores = new ArrayList<>();
playerScores.add(95);
playerScores.add(88);
playerScores.add(76);
playerScores.add(70); // no problem — ArrayList resizes automatically
playerScores.remove(0); // removes the first element (95)
System.out.println(playerScores.size()); // 3
System.out.println(playerScores.get(0)); // 88
}
}
The array rejects the fourth element at runtime with an index exception; the ArrayList absorbs it and even lets you remove from the middle — that flexibility is the whole reason it exists, and primitive values such as int are boxed as Integer elements.
Common Mistakes & Pitfalls
- Mixing up
lengthandsize()— arrays use the fieldarr.length;ArrayListuses the methodlist.size(). Callingarr.size()orlist.lengthis a compile error and a classic exam trap. - Trying
ArrayList<int>— generics only accept reference types, so it must beArrayList<Integer>. Autoboxing converts theint, but storing millions of boxedIntegers costs far more memory than anint[]. - Array covariance bites at runtime —
Object[] arr = new String[3]; arr[0] = 42;compiles fine but throwsArrayStoreExceptionat runtime, because the real array is aString[]. Generics close this hole: aList<String>simply won't let you add anIntegerat compile time. list.remove(2)on anArrayList<Integer>— this removes the element at index 2, not the value2. To delete the value you must calllist.remove(Integer.valueOf(2)).
Check your understanding
Answer these without looking back at the article.
- How does ArrayList resize itself internally?
- What is autoboxing in Java and when does it happen?
- What is the time complexity of ArrayList.get() vs add()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.