Leepoint Java Reference Notes on the Java language and its standard library.

Arrays

Fixed length, decided when you create it, and never changing.

int[] counts = new int[10];              // ten zeros
String[] names = {"Ada", "Grace"};       // length 2

The length is fixed at creation. “Resizing” means making a new array and copying — Arrays.copyOf does it.

Put the brackets with the type, not the variable. int[] a reads as “array of int”; int a[] is legal, inherited from C, and mixes badly when declaring several variables at once.

Defaults

A new array is filled in: 0 for numeric types, false for boolean, the null character for char, and null for any object type. That last one matters — new String[5] holds five nulls, not five empty strings, and looping over it without checking throws.

length

length is a field, not a method:

counts.length         // no parentheses
"hello".length()      // parentheses — String is different

Valid indices run 0 to length - 1. Outside that throws ArrayIndexOutOfBoundsException, which at least fails immediately rather than quietly reading adjacent memory.

Iterating

for (String name : names) { ... }                 // no index needed
for (int i = 0; i < names.length; i++) { ... }    // index needed

Use the first unless you need the index or need to write back — assigning to the for-each variable changes nothing.

java.util.Arrays

Most of what you want is there:

Arrays.sort(values);
Arrays.fill(counts, -1);
Arrays.toString(values);           // for printing
int[] bigger = Arrays.copyOf(values, 20);
int[] part = Arrays.copyOfRange(values, 2, 5);
boolean same = Arrays.equals(a, b);

Two worth knowing about specifically.

Printing. System.out.println(array) prints something like [I@1b6d3586 — the type and a hash code. Use Arrays.toString, or Arrays.deepToString for nested arrays.

Equality. a == b compares references, and a.equals(b) does the same thing, because arrays do not override it. Arrays.equals compares contents; Arrays.deepEquals for nested arrays.

Arrays or a List?

Use an array for a fixed number of primitives where memory or speed genuinely matters. Use ArrayList for almost everything else — it grows, and it works with the rest of the collections library.