7.1 One-Dimensional Arrays
In Java, an array is a first-class heap-allocated object rather than a raw pointer to contiguous memory as it is in C. This architecture delivers critical guarantees:
- Built-in
.lengthField: Every array automatically carries an immutable public final.lengthfield storing its exact capacity, eliminating the need to pass array sizes separately to methods. - Runtime Bounds Checking: Accessing an invalid index throws an
ArrayIndexOutOfBoundsExceptionimmediately, preventing unauthorized memory access or buffer overflow vulnerabilities. - Automatic Zero-Initialization: Heap arrays are zeroed out upon allocation (numbers become
0/0.0, booleansfalse, and object referencesnull).
int[] arr = {10, 20, 30, 40, 50};
System.out.println(arr.length); // 5
System.out.println(arr[2]); // 30
💡 Placement & Interview Deep-Dive
- What is the difference between
lengthandlength()in Java?lengthis a public final field on Java arrays (e.g.arr.length).length()is a method onjava.lang.String(e.g.str.length()). Collections usesize(). - Does
array.clone()perform a shallow copy or deep copy? It performs a shallow copy. For primitive arrays, values are copied. For arrays of objects, only the reference pointers are duplicated, pointing to the exact same heap instances.
Insertion Shifting & Two-Pointer Technique
Because Java arrays have fixed sizes upon allocation, inserting an element requires creating a larger array or shifting existing elements right, costing O(n) operations:
// Inserting 99 at index 2 requires shifting right
for (int i = n - 1; i > 2; i--) {
arr[i] = arr[i - 1];
}
arr[2] = 99;
Two-Pointer Pair Sum: On a sorted array, two moving pointers find pairs summing to a target in O(n) time instead of O(n²):
int left = 0, right = arr.length - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == target) return new int[]{left, right};
else if (sum < target) left++;
else right--;
}
7.2 Multidimensional and Jagged Arrays
A two-dimensional array in Java (declared as int[][] matrix) is not a single contiguous matrix in memory; it is an array of arrays. The outer array holds reference pointers to independent one-dimensional array objects on the heap.
Jagged (Ragged) Arrays
Because each row is an independent heap object, Java naturally supports jagged arrays, where different rows have completely different lengths:
int[][] jagged = {
{1, 2, 3}, // Row 0 has 3 elements
{4, 5} // Row 1 has only 2 elements
};
7.3 Arrays of Objects
When an array of objects is declared, such as Student[] class1 = new Student[30];, it is vital to realize what this allocation does — and does not do:
It creates an array containing 30 empty reference slots, each initialized to null. No actual Student objects are created yet!
Student[] list = new Student[3];
// list[0].name = "Bob"; // CRASH! Throws NullPointerException!
// Proper initialization:
for (int i = 0; i < list.length; i++) {
list[i] = new Student(); // Allocate the actual object!
}
list[0].name = "Bob"; // Safe!
One of the most common runtime errors in Java, thrown whenever code tries to call a method or access a field on a reference that currently points to nothing (null) — such as an unfilled slot in an array of objects.