› Module 7: Arrays › 7.3 Arrays of Objects
M O D U L E

Module 7: Arrays

Heap array objects, automatic bounds checking, multidimensional arrays, jagged arrays, and arrays of object references.

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 .length Field: Every array automatically carries an immutable public final .length field storing its exact capacity, eliminating the need to pass array sizes separately to methods.
  • Runtime Bounds Checking: Accessing an invalid index throws an ArrayIndexOutOfBoundsException immediately, preventing unauthorized memory access or buffer overflow vulnerabilities.
  • Automatic Zero-Initialization: Heap arrays are zeroed out upon allocation (numbers become 0 / 0.0, booleans false, and object references null).
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 length and length() in Java? length is a public final field on Java arrays (e.g. arr.length). length() is a method on java.lang.String (e.g. str.length()). Collections use size().
  • 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
};
m[0]: 1 2 3 row 0 has 3 elements m[1]: 4 5 row 1 has only 2 — a jagged row
Figure 7.2 — Java 2D arrays are arrays-of-arrays, allowing jagged (uneven) rows.

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!
KEY TERM: NullPointerException

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.

MODULE COMPREHENSIVE QUIZ

Module 7 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 7 Final Quiz →
Yukti Mascot