Part 04 / Topic 1 of 10
Arrays: a fixed-size box of same-type values
An array is a block of memory that holds a fixed number of values, all of the same type, sitting one after another. The number of slots — the length — is fixed the moment the array is created. You can change what is in a slot; you can never add a slot or remove one.
Think of a tray of 4 chai glasses at a Pune tea stall: exactly 4 slots, always 4 slots. You can refill a glass, but you can't wish a 5th slot into the same tray.
int[] prices = {1200, 450, 1999, 80}; // an array literal: 4 slots, decided now
System.out.println(prices.length); // 4 — not a method call, no ()
prices.lengthis a field, not a method — no().- The index of the first slot is
0; the last slot islength - 1. new int[5]makes 5 slots, each starting at the type's default:0forint,0.0fordouble,falseforboolean,nullfor any object type (String[], etc.).
int[] bill = new int[3]; // {0, 0, 0}
String[] names = new String[2]; // {null, null}
Full program: examples/01_array_basics.java.
Done here? The practice set for Part 4 — including the solutions — is on the part page.