Java arrays are fixed-length, indexed collections with a component type: int[] scores = new int[3];. Length is immutable after creation—use ArrayList when you need growth.
Declaration and initialization
int[] nums = {10, 20, 30};
String[] names = new String[2];
names[0] = "Ada";
Default values: 0 for numeric types, false for boolean, null for references.
Length and iteration
for (int i = 0; i < nums.length; i++) { }
for (int n : nums) { }
length is a field, not a method—contrast with ArrayList.size().
Important interview questions and answers
- Q: Array vs ArrayList?
A: Array fixed size, primitives allowed; ArrayList resizable, holds objects (with autoboxing). - Q: What happens on out-of-bounds access?
A:ArrayIndexOutOfBoundsExceptionat runtime.
Self-check
- How do you get an array's length?
- When must you prefer ArrayList over array?
Pitfall: length is a field on arrays but size() is a method on ArrayList—do not mix them up in interviews.