Overview
Contiguous heap storage, 1D operations, 2D matrices, and low-level JVM memory mechanics in Java
An array is a fixed-size, zero-indexed sequence of elements allocated contiguously in heap memory. Unlike the Collections Framework (ArrayList, HashSet), arrays are fundamental Java language constructs capable of storing primitives with zero boxing overhead as well as object references.
Roadmap
The array topics are organized from practical syntax and algorithms to low-level JVM memory mechanics:
Topics
1D Arrays & Operations
Declaration, default initialization values, 1D printing methods, cloning, System.arraycopy, and java.util.Arrays toolkit
2D Arrays & Matrices
Array-of-arrays heap structure, rectangular grids, jagged Pascal's triangle, 2D printing, matrix transformations, and deep cloning
Memory Layout & Internals
HotSpot JVM 16-byte object header, Mark Word, Klass Pointer, CPU cache line prefetching, covariant typing, and memory profiling
java.util.Arrays Toolkit (Quick Reference)
The primary static toolkit provided by java.util.Arrays for searching, sorting, comparing, and manipulating arrays:
| Method / Property | Parameters | Return Type | Description / Notes |
|---|---|---|---|
arr.length | (property) | int | Public final field (NOT a method). Stored in JVM object header; read via arraylength bytecode |
Arrays.sort(arr) | (int[] | double[] | ...) | void | Sorts primitives via Dual-Pivot Quicksort (O(n log n)) |
Arrays.sort(T[] a, Comparator c) | (T[] a, Comparator c) | void | Sorts objects via TimSort (O(n log n), stable) |
Arrays.parallelSort(arr) | (int[] | double[] | ...) | void | Multi-core ForkJoinPool sort for arrays > 8192 items |
Arrays.binarySearch(arr, key) | (arr, key) | int | Precondition: array must be sorted. Returns index or -(insertion_point) - 1 |
Arrays.fill(arr, val) | (arr, val) | void | Assigns val to every slot |
Arrays.equals(a, b) | (a, b) | boolean | Element-by-element equality check |
Arrays.deepEquals(a, b) | (Object[], Object[]) | boolean | Deep recursive equality check for multidimensional arrays |
Arrays.toString(arr) | (arr) | String | Formats 1D array as "[e1, e2, ...]"; avoids hashcode [I@... |
Arrays.deepToString(arr) | (Object[]) | String | Formats multidimensional 2D/3D arrays recursively |
Arrays.mismatch(a, b) | (a, b) | int | Returns index of first differing element, or -1 if identical (Java 9+) |
Arrays.stream(arr) | (arr) | IntStream / Stream<T> | Produces primitive or object stream |
Arrays.copyOf(arr, newLen) | (arr, int) | T[] | Returns truncated or zero-padded copy |
Arrays.copyOfRange(arr, from, to) | (arr, int, int) | T[] | Half-open slice copy [from, to) |
Arrays.asList(T... a) | (T... a) | List<T> | Fixed-size list wrapper backed by the array |
Quick Reference: 1D vs 2D vs Memory
| Concept | 1D Arrays | 2D Arrays & Matrices | Memory & JVM Internals |
|---|---|---|---|
| Physical Layout | Single contiguous memory block | Array of reference pointers to disjoint 1D arrays | 16-byte object header + aligned payload |
| Indexing | arr[i] (O(1) pointer offset) | matrix[r][c] (two dereferences) | Byte offset: base + 16 + (i * elementSize) |
| Length Field | arr.length | Rows: matrix.length, Cols: matrix[0].length | Stored as signed 32-bit int in header |
| Printing | Arrays.toString(arr) | Arrays.deepToString(matrix) | Default Object.toString() returns [I@hash |
| Cloning | arr.clone() is deep for primitives | matrix.clone() is a shallow copy trap | Only clones the outer row pointer array |
Cheatsheet
Master quick-reference lookup tables for Arrays, Strings, Lists, Maps, Sets, and Queues with return types, parameters, and time complexities
1D Arrays & Operations
Declaration, instantiation, comprehensive printing methods, static algorithm utilities, in-place manipulation, and LeetCode patterns