Arrays in Java

From the Java cheat sheet · Arrays & Collections · verified Jul 2026

Arrays

Fixed-size sequences and the Arrays utility class.

java
// Quick Reference
int[] nums = {1, 2, 3};
int[] zeros = new int[5];
nums.length;            // 3
Arrays.sort(nums);
💡 Array length is fixed at creation and read via .length (a field, no parentheses).
⚡ Arrays.toString() prints contents; plain toString() prints a memory hash.
📌 Accessing an out-of-range index throws ArrayIndexOutOfBoundsException.
🟢 For a resizable sequence, reach for ArrayList instead of a raw array.
arrays

More Java tasks

Back to the full Java cheat sheet